如何获得更多的领域

时间:2012-01-16 21:40:21

标签: cakephp cakephp-2.0

我的查询只返回library_id。我需要在我的视图中显示标题。如何才能获得要显示的标题。 这是我的分享模式

public $belongsTo = array(
        'Person' => array(
            'className' => 'Person',
            'foreignKey' => 'person_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
        'Library' => array(
            'className' => 'Library',
            'foreignKey' => 'library_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );

这是我的图书馆模型

class Library extends AppModel {

    //The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * hasMany associations
 *
 * @var array
 */
    public $hasMany = array(
        'Share' => array(
            'className' => 'Share',
            'foreignKey' => 'library_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => 'Title',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        ),
        'Work' => array(
            'className' => 'Work',
            'foreignKey' => 'library_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
    );

}

这是我在控制器中的添加方法

public function add() {
        if ($this->request->is('post')) {
            $this->Share->create();
            if ($this->Share->save($this->request->data)) {
                $this->Session->setFlash(__('The share has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The share could not be saved. Please, try again.'));
            }
        }
        $people = $this->Share->Person->find('list');
        $libraries = $this->Share->Library->find('list');
        $this->set(compact('people', 'libraries'));
    }

这是我的观点

<div class="shares form">
<?php echo $this->Form->create('Share');?>
    <fieldset>
        <legend><?php echo __('Add Share'); ?></legend>
    <?php
        echo $this->Form->input('person_id');
    echo $this->Form->input('library_id');
        echo $this->Form->input('share');

    ?>
    </fieldset>
<?php echo $this->Form->end(__('Submit'));?>
</div>

1 个答案:

答案 0 :(得分:0)

不要紧,上面的问题我需要做的就是

$ libraries = $ this-&gt; Share-&gt; Library-&gt; find('list',array('fields'=&gt;'Library.Title'));

LibrariesController中的

感谢