0

外部キーを持つユーザーのモデルを表すクラスは、 id of picture です。

class Model_User extends Model_AbstractEntity
{
    protected $u_id;
    protected $u_email;
    protected $u_firstname;
    protected $u_lastname;
    protected $u_password;
    protected $u_salt;
    protected $u_created_at;
    protected $u_updated_at;
    protected $u_fb;
    protected $u_status;
    protected $u_r_id;
    protected $u_p_id;
}

画像モデルを担当するクラスは次のようになります。

class Model_Picture extends Model_AbstractEntity
{
    protected $p_id;
    protected $p_created_at;
    protected $p_updated_at;
    protected $p_caption;
    protected $p_name;
    protected $p_basePath;
    protected $p_available;
    protected $p_u_id;
}

これは、データベースからデータを取得する唯一のモデル パーツです。Foreingキーはu_p_idで、画像のキーはp_idです私の問題は、Zend dbテーブルでselect()を実行すると、外部キーを含むデータが返されることですが、返されたデータのどの部分が画像部分であるかを知るにはどうすれば適切な画像モデルを設定できますか. ... 適切な方法でそれを行う方法はありません 2 つのクエリを実行し、1 つはユーザー用、もう 1 つは画像用で、2 つの連想オブジェクトを作成します。私は今、1対1の関係について話していますが、おそらく1対多になるでしょう..

4

1 に答える 1

1

通常、エンティティ モデルは無効には存在せず、ある種のデータ マッパー モデルと連携して存在します。Mapper は通常、便利なソースからデータを収集し、エンティティ モデルを構築します。

たとえば、アルバム エンティティを持つ音楽コレクションがあります。

<?php
class Music_Model_Album extends Model_Entity_Abstract implements Interface_Album
{
    //id is supplied by Entity_Abstract
    protected $name;
    protected $art;
    protected $year;
    protected $artist; //alias of artist_id in Database Table, foreign key
    protected $artistMapper = null;

    /**
     * Constructor, copied from Entity_Abstract
     */
    //constructor is called in mapper to instantiate this model
    public function __construct(array $options = null)
    {         
        if (is_array($options)) {
            $this->setOptions($options);
        }
    }

  /**
   * Truncated for brevity.
   * Doc blocks and normal getters and setters removed
   */


    public function getArtist() {
        //if $this->artist is set return
        if (!is_null($this->artist) && $this->artist instanceof Music_Model_Artist) {
            return $this->artist;
        } else {
            //set artist mapper if needed
            if (!$this->artistMapper) {
                $this->artistMapper = new Music_Model_Mapper_Artist();
            }
            //query the mapper for the artist table and get the artist entity model
            return $this->artistMapper->findById($this->getReferenceId('artist'));
        }
    }
    //set the artist id in the identity map
    public function setArtist($artist) {
        //artist id is sent to identity map. Can be called later if needed - lazy load
        $this->setReferenceId('artist', $artist);
        return $this;
    }


    //each album will have multiple tracks, this method allows retrieval as required.
    public function getTracks() {
        //query mapper for music track table to get tracks from this album
        $mapper = new Music_Model_Mapper_Track();
        $tracks = $mapper->findByColumn('album_id', $this->id, 'track ASC');

        return $tracks;
    }
}

マッパーでは、次のようなエンティティ モデルを構築します。

 //excerpt from Model_Mapper_Album
 //createEntity() is declared abstract in Model_Mapper_Abstract
 public function createEntity($row)
    {
        $data = array(
            'id'     => $row->id,
            'name'   => $row->name,
            'art'    => $row->art,
            'year'   => $row->year,
            'artist' => $row->artist_id,//
        );

        return new Music_Model_Album($data);
    }

このメソッドをマッパー メソッドで使用するには、次のようにします。

 //this is actually from Model_Mapper_Abstract, b ut give the correct idea and will work in any of my mappers.
 //this returns one and only one entity
 public function findById($id)
    {
        //if entity id exists in the identity map
        if ($this->getMap($id)) {
            return $this->getMap($id);
        }
        //create select object
        $select = $this->getGateway()->select();
        $select->where('id = ?', $id);
        //fetch the data
        $row = $this->getGateway()->fetchRow($select);
        //create the entity object
        $entity = $this->createEntity($row);
        //put it in the map, just in case we need it again
        $this->setMap($row->id, $entity);
        // return the entity
        return $entity;
    }

エンティティとマッパーがさまざまな方法で構築されているのを見てきました。好きな方法を見つけて楽しんでください。

質問には実際には当てはまらないため、多くのコードがこのデモから除外されています。完全なコードを表示する必要がある場合は、GitHub で参照してください。

于 2012-11-08T09:08:01.110 に答える