cakephp从多个相关表中读取和显示

时间:2012-11-03 12:14:00

标签: php cakephp cakephp-2.1 cakephp-appmodel

我有2个表用户和艺术家。

User:
id
name
password
city_id
state_id
country_id

Artist:
id
user_id
description

我希望创建添加和编辑表单(单个表单充当添加和编辑)。但是点击编辑链接,我希望填写用户和艺术家表格中的表单字段。我怎么做?以下是我的控制器代码:

public function artist_register() {
        $this->country_list();
        if ($this->request->is('post')) {
            $this->request->data['User']['group_id'] = 2;
            if ($this->{$this->modelClass}->saveAll($this->request->data)) {
                $this->redirect(array('controller' => 'Users', 'action' => 'login'));
            }
        }
        $this->render('artist_update');
    }

    public function artist_update($id) {
        $artist = $this->{$this->modelClass}->findByuser_id($id);
        $artist_id = $artist[$this->modelClass]['id'];

        if($this->request->is('get')) {
            $this->request->data = $this->{$this->modelClass}->read(null, $artist_id);
            $this->request->data = $this->{$this->modelClass}->User->read(null, $id);
        } else {
            if($this->{$this->modelClass}->save($this->request->data)) {
                $this->Session->setFlash('Your Profile has been updated');
                $this->redirect(array('action' => 'index'));
            }
        }
    }

代码不起作用。我该如何解决?

1 个答案:

答案 0 :(得分:1)

要做的第一件事:

您必须定义UserArtist之间的关系。

A user has one artist
A user has many artist
A artist belongs to an user

在这种情况下,我认为您正在寻找的关系是:

A artist belongsTo user

编码时间:

了解这一点,您现在可以定义关系。在Artist模型中,您需要声明belongsTo变量:

class Artist extends AppModel {
    public $belongsTo = 'User';
}

或者你可以设置这样的关系:

class Artist extends AppModel {
    public $belongsTo = array(
        'User' => array(
            'className'    => 'User',
            'foreignKey'   => 'user_id'
        )
    );
}

供您参考:

belongsTo关联中可以使用另一种可能的密钥:

  • className :与当前模型关联的模型的类名。如果您要定义'Artist belongsTo User'关系,则className键应该等于'User。'

  • foreignKey :在当前模型中找到的外键的名称。如果您需要定义多个belongsTo关系,这尤其方便。此键的默认值是另一个模型的下划线,单数名称,后缀为_id。

  • 条件:一组find()兼容条件或SQL字符串,例如数组(' User.active' => true)

    < / LI>
  • 类型:在SQL查询中使用的联接类型,默认为LEFT,可能无法满足您在所有情况下的需求,当您需要来自您的所有内容时,INNER可能会有所帮助主要和相关的模型或什么都没有! (当然在某些条件下使用时有效)。 (注意:类型值为小写 - 即左,内)

  • 字段:获取关联模型数据时要检索的字段列表。默认返回所有字段。

  • 订单:find()兼容订单子句或SQL字符串(如数组)的数组(&#39; User.username&#39; =&gt;&#39; ASC&# 39)

会发生什么?

这是定义我们关系后$this->Artist->find()调用的结果。

Array
(
    [Artist] => Array
    (
        [id] => 5
        [user_id] => 1
        [description] => I am an artist!
    )
    [User] => Array
    (
        [id] => 1
        [name] => Fuhrmann
        [passsword] => 1234
        [city_id] => 500
        [state_id] => 2020
        [country_id] => 223
    )

)

那又怎样?

完成所有这些操作后,您可以在查找User时访问Artist

public function artist_update($id) {
    // Read the artist and the user info
    $artist = $this->{$this->modelClass}->findByUserId($id);        
    $this->set(compact('artist'));
}

在您看来,您可以在Users表格中获取用户名或其他字段:

<input type="text" name="name" value="<?php echo $artist['User']['name'];?>"
<input type="text" name="user_id" value="<?php echo $artist['User']['id'];?>"
<input type="text" name="country_id" value="<?php echo $artist['User']['country_id'];?>"

了解更多

详细了解Associations: Linking Models Together

相关问题