我有三个表用户,属性和user_attributes。
users[id, name, username, password, dob, gender, isactive, registered_on],
attributes[id, name, isactive],
user_attributes[id, user_id, attribute_id]
这是我的表格结构。现在我想编写一个查询,如果user_attributes表中存在任何行,将从user表中获取所有用户以及user_attributes表的值。
如何在cakephp中编写查询?
我有两个模型User
和Attribute
。
提前致谢。
答案 0 :(得分:0)
请按照以下步骤操作:
1 - 为表user_attributes创建Model UserAttribute。
class UserAttribute extends AppModel{
}
2-在您的用户模型中,创建与UserAttribute
的hasMany关联class User extends AppModel{
public $hasMany = array(
'UserAttribute' => array(
'className' => 'UserAttribute',
'foreignKey' => 'user_id',
),
);
}
3-在控制器中查找查询:
$users= $this->User->find('all');
echo "<pre>";
print_r($users);