访问外部表CakePHP
如果标题有点混乱,我很抱歉,我只是不知道如何正确地称呼它。我的CakePHP项目有这样一个表结构 用户id、姓名、姓氏、用户记录 userrecordsid,user\u id,records\u id 记录id,说明 我知道要访问我的用户视图中的用户记录中间表,我必须执行以下操作访问外部表CakePHP,php,cakephp,Php,Cakephp,如果标题有点混乱,我很抱歉,我只是不知道如何正确地称呼它。我的CakePHP项目有这样一个表结构 用户id、姓名、姓氏、用户记录 userrecordsid,user\u id,records\u id 记录id,说明 我知道要访问我的用户视图中的用户记录中间表,我必须执行以下操作 $user['userrecords']['id']; 如何通过用户视图灵活地访问记录表中的说明?阅读此内容,可能会对您有所帮助 阅读此文,可能会对您有所帮助 阅读此文,可能会对您有所帮助 阅读此文,可能会对您有所
$user['userrecords']['id'];
如何通过用户视图灵活地访问记录表中的说明?阅读此内容,可能会对您有所帮助
阅读此文,可能会对您有所帮助
阅读此文,可能会对您有所帮助
阅读此文,可能会对您有所帮助
因为您的表结构如下所示: 用户->用户记录->记录 因此,您只能通过[UserRecord]获取[Record] 您应该在find命令中设置递归属性 有关递归的更多信息,请参阅此链接:
我希望这个答案不会误解您的问题。因为您的表格结构如下: 用户->用户记录->记录 因此,您只能通过[UserRecord]获取[Record] 您应该在find命令中设置递归属性 有关递归的更多信息,请参阅此链接:
我希望这个答案不会误解您的问题。因为您的表格结构如下: 用户->用户记录->记录 因此,您只能通过[UserRecord]获取[Record] 您应该在find命令中设置递归属性 有关递归的更多信息,请参阅此链接:
我希望这个答案不会误解您的问题。因为您的表格结构如下: 用户->用户记录->记录 因此,您只能通过[UserRecord]获取[Record] 您应该在find命令中设置递归属性 有关递归的更多信息,请参阅此链接:
我希望这个答案不会误解您的问题。您没有指定使用的是CakePHP 2.x还是3.x,因此我提供了这两种解决方案 您所指的关系称为“具有并属于多个”关系。由于两个模型都与链接表(userrecords)相关联,因此您可以自由地将任意多个记录与任意多个用户关联
首先,我会把你的“用户记录”表改名为“UsSersRead”,以便与CAKEPHP很好地匹配。< /P> 首先,在用户模型中定义您的关系:
// 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
]);
}
}
现在,我们必须在记录模型中定义我们的关系:
// 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的contain方法从每个模型自由访问相关记录:
// 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还是3.x,所以我提供了这两种解决方案 您所指的关系称为“具有并属于多个”关系。由于两个模型都与链接表(userrecords)相关联,因此您可以自由地将任意多个记录与任意多个用户关联
首先,我会把你的“用户记录”表改名为“UsSersRead”,以便与CAKEPHP很好地匹配。< /P> 首先,在用户模型中定义您的关系:
// 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
]);
}
}
现在,我们必须在记录模型中定义我们的关系:
// 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的contain方法从每个模型自由访问相关记录:
// 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还是3.x,所以我提供了这两种解决方案 您所指的关系称为“具有并属于多个”关系。由于两个模型都与链接表(userrecords)相关联,因此您可以自由地将任意多个记录与任意多个用户关联
首先,我会把你的“用户记录”表改名为“UsSersRead”,以便与CAKEPHP很好地匹配。< /P> 首先,在用户模型中定义您的关系:
// 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
]);
}
}
现在,我们必须在记录模型中定义我们的关系:
// 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的contain方法从每个模型自由访问相关记录:
// 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还是3.x,所以我提供了这两种解决方案 您所指的关系称为“具有并属于多个”关系。由于两个模型都与链接表(userrecords)相关联,因此您可以自由地将任意多个记录与任意多个用户关联
首先,我会把你的“用户记录”表改名为“UsSersRead”,以便与CAKEPHP很好地匹配。< /P> 首先,在用户模型中定义您的关系:
// 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
]);
}
}
现在,我们必须在记录模型中定义我们的关系:
// 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的contain方法从每个模型自由访问相关记录:
// 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();
阅读烹饪书,它会让你的生活轻松一百万倍: