ItGo.me Focus on IT Recommend

Home > has and belongs to many - Need help with hasAndBelongsToMany (HABTM) Cakephp find

has and belongs to many - Need help with hasAndBelongsToMany (HABTM) Cakephp find

2020腾讯云双十一活动,全年最低!!!(领取3500元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1074

【阿里云】双十一活动,全年抄底价,限时3天!(老用户也有),
入口地址https://www.aliyun.com/1111/home

I am trying to query a hasAndBelongsToMany relationship in Cakephp 1.3, but it looks like the SQL query being run is not doing a Join on the many to many table.

I have a users table, projects table, and users_projects table. I want to get a list of all projects a user is associated with in a separate Allocations controller.

I have been reading the cakephp book on the topic: http://book.cakephp.org/view/1044/hasAndBelongsToMany-HABTM

So I have been trying this:

var_dump($this->Allocation->User->Project->find('list',array('conditions'=>array('Project.user_id'=>'21'))));

However that does not work, It returns nothing and this error:

Warning (512): SQL Error: 1054: Unknown column 'Projects.user_id' in 'where clause' [CORE/cake/libs/model/datasources/dbo_source.php, line 681]

Query: SELECT `Project`.`id`, `Project`.`name` FROM `projects` AS `Project` WHERE `Projects`.`user_id` = '21' 

It looks like cakephp is not doing the required join.

This is in my projects model:

var $hasAndBelongsToMany = array(
    'User' => array(
        'className' => 'User',
        'joinTable' => 'users_projects',
        'foreignKey' => 'project_id',
        'associationForeignKey' => 'user_id',
        'unique' => true,
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'finderQuery' => '',
        'deleteQuery' => '',
        'insertQuery' => ''
    )

And this is in my users model:

Recommend:has and belongs to many - CakePHP HABTM question

ree time. For starters I want to add multiple tags to multiple photos. I have a tags model and mot model (the photos). Snip of mot model: var $hasAndBelongsToMany = array( 'Tag' =>

var $hasAndBelongsToMany = array(
    'Project' => array(
        'className' => 'Project',
        'joinTable' => 'users_projects',
        'foreignKey' => 'user_id',
        'associationForeignKey' => 'project_id',
        'unique' => true,
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'finderQuery' => '',
        'deleteQuery' => '',
        'insertQuery' => ''
    )

So, what is the correct syntax to get the list of projects for a given user on a many to many relationship in cakephp?

cakephp has-and-belongs-to-many
|
  this question
asked Sep 16 '10 at 18:38 lanrat 1,037 5 20 32

 | 

2 Answers
2

---Accepted---Accepted---Accepted---

You are getting this error because you are using the find type 'list'. Cake's default behaviour is to select the ID and NAME fields when using this find type.

Change your find type to 'all' to retrieve the results you are expecting:

var_dump($this->Allocation->User->Project->find('list',array('conditions'=>array('Project.user_id'=>'21'))));


|
  this answer
answered Sep 16 '10 at 18:45 ABailiss 138 1 1 9      When changing to all I still get the same error. Here is the SQL output: Query: SELECT Project. id, Project. name, Project. description, Project. created, Project. modified FROM projects AS Project WHERE Projects. user_id = '21' –  lanrat Sep 16 '10 at 18:48      I also want the returned data be be in a list format to use in a html form. –  lanrat Sep 16 '10 at 18:50      Have you also changed the 'joinTable' => 'users_projects' after renaming your join table in both the User and Project models? –  ABailiss Sep 18 '10 at 10:22

 | 

Perhaps that's because you didn't make the two table names connected in alphabetical order.users_projects should be projects_users.


|
  this answer
answered Sep 17 '10 at 6:09 Young 4,270 5 31 53      This helped, but the code is still not doing a join. –  lanrat Sep 17 '10 at 18:42      @mrlanrat,but what do you want your query result look like? –  Young Sep 19 '10 at 0:29      I want just the Projects and IDs of the projects associated with the given user. –  lanrat Sep 19 '10 at 19:23      @mrlanrat,you already did that since you've specified Project.user_id. –  Young Sep 20 '10 at 0:10      I lost 4 hours , just that a name of my table of hasandbelongstomany was inversed .... GOD SAVE AS –  Skynet Sep 15 '15 at 22:36

 | 

Recommend:has and belongs to many - CakePHP find HABTM

e Message belongsto User Message HABTM Tag Tag HABTM Message If a User is logged in he might want to see all Message tagged with something. $messages = $this->Message->find('all', array( 'conditions' => array("Message.user_id" => $this-

------splitte line----------------------------