3

I've 3 tables: project, amenities and project_amenities.

I add project form only, i want to add new amenities, which first added to amenities table and then project_amenities table.

So I defined these relations:

Project hasMany Amenities

Amenity belongsTo Project

I declared in projects_controller: var $uses = array('Amenity');

This gives error for my add action here

if (!empty($this->data)){

    $this->Project->create();

as

Notice (8): Undefined property: ProjectsController::$Project 
[APP\controllers\projects_controller.php, line 60]

edit Also, I cannot do:

$this->Amenity->create();
$insertData = array('name' => $this->data['Project']['Amenity'][$i]['name']);
$this->Amenity->save($insertData);
Ross
  • 18,117
  • 7
  • 44
  • 64
vibha
  • 129
  • 3
  • 13
  • also, I cant do this, $this->Amenity->create(); $insertData = array('name' => $this->data['Project']['Amenity'][$i]['name']); $this->Amenity->save($insertData); – vibha Jul 13 '11 at 12:34
  • Judging by your tables' names, you might want to change your `Project<->Amenity` association to [HABTM](http://book.cakephp.org/view/1044/hasAndBelongsToMany-HABTM), renaming `project_amenities` to `projects_amenities`. – lxa Jul 13 '11 at 14:00
  • yes renamed project_amenities to projects_amenities...also changed relation in Amenities as hasMany Projects..still insert in this table is not working – vibha Jul 14 '11 at 06:26

4 Answers4

6

I wouldn't recommend using $uses = array() as it defies the purpose of having model relationships. Plus it adds a fair bit of overhead too.

You should use your model relationships where ever possible:

$this->Project->create();
$this->Project->Amenity->create();
$this->Model1->Model2->Model3->Model4->create();

If you want to use an unrelated model, you can do:

$this->loadModel('NewModelName');
$this->NewModelName->action();

Check out http://book.cakephp.org/view/1031/Saving-Your-Data and http://book.cakephp.org/view/992/loadModel

Shaz MJ
  • 1,785
  • 1
  • 12
  • 25
3

I would like to suggest you to use $this->loadModel('ModelName'); in the function where you needed. Then you can use the model as below

$this->ModelName->function();

By using $uses array it make available to entire controller. Which will affect your performance.

Vins
  • 8,849
  • 4
  • 33
  • 53
0

you must specify the project model in your $uses variable:

$uses = array('Project','Amenity');

but I'm curious why do you have three tables when you need one to many relationship?

Headshota
  • 21,021
  • 11
  • 61
  • 82
  • seems to me that its a NxN relationship because of the intermediary table `project_amenities` – pleasedontbelong Jul 13 '11 at 12:45
  • yeap but OP doesn't use the third table... as he described, it's one-to-many relation – Headshota Jul 13 '11 at 12:55
  • This is the reason for your error, but you should not need to, as Cake makes these associations automatically based on the keys within your tables. Do you have `project_id` in your amenity table? – Ross Jul 13 '11 at 13:29
  • Only clarifying what I think was already said: you DO NOT need the $uses variable. – Beto Frega Jul 13 '11 at 13:34
  • Dont i need $uses,if i"ve many to many relationship for project_amenities table? I've changed relation in Amenities as hasMany Projects. Then how can I add entries in this table... – vibha Jul 14 '11 at 06:20
  • If I remove $uses, then it gives error for Amenity. Also Why this is not working for($i = 0; $i< count($this->data['Project']['Amenity']);$i++){ $this->Amenity->create(); $insertData = array('name' => $this->data['Project']['Amenity'][$i]['name']); $amenity = $this->Amenity->save($insertData); $data['AmenityArr'] = array( 'project_id' => $this->Project->getLastInsertID(), 'amenity_id' => $this->Amenity->getLastInsertID() ); $this->Project->Amenity->save($data['AmenityArr']); – vibha Jul 14 '11 at 06:26
0

Solution for :save for hasAndBelongsToMany

I created model: projects_amenity.php which has relation belongsTo project and amenity. model: projects has relation hasAndBelongsToMany with amenity controller: projects_amenities_controller

and in code:

$this->loadModel('ProjectsAmenity');
for($i = 0; $i< count($this->data['Project']['Amenity']);$i++)
{                       
    $this->Amenity->create();   

    $insertData = array('name' => $this->data['Project']['Amenity'][$i]['name']);
    $amenity = $this->Amenity->save($insertData);   

    $amenityid = $this->Amenity->getLastInsertID();

    $projectid = $this->Project->getLastInsertID();

    $this->ProjectsAmenity->create();           
    $data['AmenityArr']  = array('project_id' =>  $projectid,'amenity_id' => $amenityid );

    $this->ProjectsAmenity->save($data['AmenityArr']);  
}
Nunser
  • 4,512
  • 8
  • 25
  • 37
vibha
  • 129
  • 3
  • 13