访问外部表 CakePHP
Access Foreign Tables CakePHP
对不起,如果标题有点混乱,我只是不知道如何正确称呼它。我的 CakePHP 项目有一个这样的 table 结构。
用户 id, name, surname, userrecords
用户记录 id,user_id,records_id
记录 ID、描述
我知道要在我的用户视图中访问用户记录中间table,我必须做一些事情
$user['userrecords']['id'];
如何通过用户视图[=31]巧妙地访问记录table中的描述 =]?
阅读本文,可能对您有所帮助
http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html
因为您的 table 结构如下:
用户 -> 用户记录 -> 记录
这样你只能通过[UserRecord]获取[Record]
你应该在查找命令中设置递归属性。
有关递归的更多信息,请参阅此link:what is the meaning of recursive in cakephp?
希望这个回答不会误解您的问题。
您没有指定您使用的是 CakePHP 2.x 还是 3.x,因此我为两者提供了解决方案。
您所指的关系称为 "Has And Belongs To Many" 关系。由于您的两个模型都与链接 table(用户记录)相关联,因此您可以自由地将任意数量的记录关联到任意数量的用户。
首先,我会考虑将您的 'userrecords' table 重命名为 'users_records' 以更好地与 CakePHP 配合使用。
首先,定义您在用户模型中的关系:
// Using CakePHP 2.x:
class User extends AppModel {
public $actsAs = array('Containable'); // Instantiate Containable behavior
public $hasAndBelongsToMany = array(
'Record' =>
array(
'className' => 'Record',
'joinTable' => 'users_records',
'foreignKey' => 'user_id',
'associationForeignKey' => 'record_id'
)
);
}
// Using CakePHP 3.x:
class UsersTable extends Table
{
public function initialize (array $config)
{
$this->belongsToMany('Records', [
'joinTable' => 'users_records' // Defines our linking table
]);
}
}
现在,我们必须在 Records 模型中定义我们的关系:
// Using CakePHP 2.x:
class Record extends AppModel {
public $actsAs = array('Containable'); // Instantiate Containable behavior
public $hasAndBelongsToMany = array(
'User' =>
array(
'className' => 'User',
'joinTable' => 'users_records',
'foreignKey' => 'record_id',
'associationForeignKey' => 'user_id'
)
);
}
// Using CakePHP 3.x:
class RecordsTable extends Table
{
public function initialize (array $config)
{
$this->belongsToMany('Users', [
'joinTable' => 'users_records' // Defines our linking table
]);
}
}
现在,我们可以使用 ORM 的包含方法从每个模型中自由访问关联的记录:
// Using CakePHP 2.x:
// Getting 'User' and associated 'Record' models in Controller:
$this->loadModel('User');
$this->User->find('all', array('contain' => 'Record'));
// Getting 'Record' and associated 'User' models in Controller:
$this->loadModel('Record');
$this->Record->find('all', array('contain' => 'User'));
// Using CakePHP 3.x:
// Getting 'User' and associated 'Record' models:
$users_table = TableRegistry::get('Users');
$users = $users_table->find()->contain('Records')->all();
// Getting 'Record' and associated 'User' models:
$records_table = TableRegistry::get('Records');
$records = $records_table->find()->contain('Users')->all();
阅读食谱,它会让你的生活轻松一百万倍:
CakePHP 2.x Containable Behavior
CakePHP 2.x Has And Belongs To Many Relationship
CakePHP 3.x belongsToMany Relationship
对不起,如果标题有点混乱,我只是不知道如何正确称呼它。我的 CakePHP 项目有一个这样的 table 结构。
用户 id, name, surname, userrecords
用户记录 id,user_id,records_id
记录 ID、描述
我知道要在我的用户视图中访问用户记录中间table,我必须做一些事情
$user['userrecords']['id'];
如何通过用户视图[=31]巧妙地访问记录table中的描述 =]?
阅读本文,可能对您有所帮助
http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html
因为您的 table 结构如下:
用户 -> 用户记录 -> 记录
这样你只能通过[UserRecord]获取[Record]
你应该在查找命令中设置递归属性。
有关递归的更多信息,请参阅此link:what is the meaning of recursive in cakephp?
希望这个回答不会误解您的问题。
您没有指定您使用的是 CakePHP 2.x 还是 3.x,因此我为两者提供了解决方案。
您所指的关系称为 "Has And Belongs To Many" 关系。由于您的两个模型都与链接 table(用户记录)相关联,因此您可以自由地将任意数量的记录关联到任意数量的用户。
首先,我会考虑将您的 'userrecords' table 重命名为 'users_records' 以更好地与 CakePHP 配合使用。
首先,定义您在用户模型中的关系:
// Using CakePHP 2.x:
class User extends AppModel {
public $actsAs = array('Containable'); // Instantiate Containable behavior
public $hasAndBelongsToMany = array(
'Record' =>
array(
'className' => 'Record',
'joinTable' => 'users_records',
'foreignKey' => 'user_id',
'associationForeignKey' => 'record_id'
)
);
}
// Using CakePHP 3.x:
class UsersTable extends Table
{
public function initialize (array $config)
{
$this->belongsToMany('Records', [
'joinTable' => 'users_records' // Defines our linking table
]);
}
}
现在,我们必须在 Records 模型中定义我们的关系:
// Using CakePHP 2.x:
class Record extends AppModel {
public $actsAs = array('Containable'); // Instantiate Containable behavior
public $hasAndBelongsToMany = array(
'User' =>
array(
'className' => 'User',
'joinTable' => 'users_records',
'foreignKey' => 'record_id',
'associationForeignKey' => 'user_id'
)
);
}
// Using CakePHP 3.x:
class RecordsTable extends Table
{
public function initialize (array $config)
{
$this->belongsToMany('Users', [
'joinTable' => 'users_records' // Defines our linking table
]);
}
}
现在,我们可以使用 ORM 的包含方法从每个模型中自由访问关联的记录:
// Using CakePHP 2.x:
// Getting 'User' and associated 'Record' models in Controller:
$this->loadModel('User');
$this->User->find('all', array('contain' => 'Record'));
// Getting 'Record' and associated 'User' models in Controller:
$this->loadModel('Record');
$this->Record->find('all', array('contain' => 'User'));
// Using CakePHP 3.x:
// Getting 'User' and associated 'Record' models:
$users_table = TableRegistry::get('Users');
$users = $users_table->find()->contain('Records')->all();
// Getting 'Record' and associated 'User' models:
$records_table = TableRegistry::get('Records');
$records = $records_table->find()->contain('Users')->all();
阅读食谱,它会让你的生活轻松一百万倍:
CakePHP 2.x Containable Behavior
CakePHP 2.x Has And Belongs To Many Relationship
CakePHP 3.x belongsToMany Relationship