Product Plan ProductPlan
id |name id |name id | product_id | plan_id
1 aplha 1 a 1 1 2
2 bravo 2 b
3 charlie
I want to find Product name and Plan name against ProductPlan, if product id and plan id for product exist in ProductPlan then the name of plan and product will show, I tried a lot, the relation b/w tables are correct but i didn't get the exact data, query which i used that is
$p_plan = $this->ProductPlan->find('all',array(
'conditions'=>array(
'ProductPlan.product_id'=>'Product.product_id'
)
)
);
$this->set('p_plan', $p_plan);
if some body help me , i'll very thaksful to him. Thanks in advance.
Relation
For Plan
class Plan extends AppModel{
public $primaryKey='plan_id';
public $hasMany = array(
'ProductPlan'=>array(
'className' => 'ProductPlan',
'foreignKey' => 'plan_id'
)
);
Product
class Product extends AppModel{
public $primaryKey='product_id';
public $hasMany = array(
'ProductsUser'=>array(
'className' => 'ProductsUser',
'foreignKey' => 'product_id'
),
'ProductsUserNode'=>array(
'className' => 'ProductsUserNode',
'foreignKey' => 'product_id'
),
'ProductPlan'=>array(
'className' => 'ProductPlan',
'foreignKey' => 'product_id'
)
);
for Product Plan
class ProductPlan extends AppModel{
var $primaryKey='product_plan_id';
public $belongsTo = array(
'Product' => array(
'className' => 'Product',
'foreignKey' => 'product_id'
),
'Plan' => array(
'className' => 'Plan',
'foreignKey' => 'plan_id'
)
);
public $hasMany = array(
'ProductPlansUserNode'=>array(
'className' => 'ProductPlansUserNode',
'foreignKey' => 'product_plan_id'
),
);
}
You should simply be able to use 'contain':-
$p_plan = $this->Product->find('all', array(
'contain' => array('Plan')
));
This will return all your products, with their associated plans. Your Product and Plan models need a hasAndBelongToMany relationship. There is no need to define a model for your joins table.
class Product AppModel {
...
public $hasAndBelongsToMany = array('Plan');
...
}
class Plan AppModel {
...
public $hasAndBelongsToMany = array('Product');
...
}
As a side note I'd personally avoid overriding CakePHP's default way of handling primary keys. It is better to stick with convention and use id.
Related
I'm having an issue with Cake joining tables in the wrong place :(
I have three tables - CC_PLAYLIST, CC_PLAYLISTCONTENTS, and CC_FILES.
I'd like to get a list of all playlists, with all of their contents, and then the relevant file information for each piece of playlist content.
I.E
Playlist 1
-- Playlist Content 1
---- File Info
-- Playlist Content 2
---- File Info
-- Playlist Content 3
---- File Info
Playlist 2
-- Playlist Content 123
---- File Info
In my Model (AirtimePlaylist) I get this data with
$result = $this->find('all', array(
'contain' => array(
'AirtimePlaylistContent' => array(
'fields' => array('AirtimePlaylistContent.id', 'AirtimePlaylistContent.playlist_id', 'AirtimePlaylistContent.file_id'),
'AirtimeFile' => array(
'fields' => array('AirtimeFile.id', 'AirtimeFile.track_title', 'AirtimeFile.artist_name')
)
),
),
'conditions' => array('AirtimePlaylist.id' => 5),
'fields' => 'AirtimePlaylist.id', 'AirtimePlaylist.name'
));
It seems however that the join between CC_PLAYLISTCONTENTS and CC_FILES is not quite playing ball. CC_PLAYLISTCONTENTS should join CC_FILES with CC_PLAYLISTCONTENTS.file_id = CC_FILES.id but it seems to be doing CC_PLAYLISTCONTENTS.id = CC_FILES.id
AirtimePlaylist.php
class AirtimePlaylist extends AppModel {
public $useTable = 'cc_playlist';
public $actsAs = array('Containable');
public $hasMany = array('AirtimePlaylistContent' => array(
'className' => 'AirtimePlaylistContent',
'foreignKey' => 'playlist_id'
));
AirtimePlaylistContent.php
class AirtimePlaylistContent extends AppModel {
public $useTable = 'cc_playlistcontents';
public $hasOne = array('AirtimePlaylist' => array(
'className' => 'AirtimePlaylist',
'foreignKey' => 'id'
),'AirtimeFile' => array(
'className' => 'AirtimeFile',
'foreignKey' => 'id'
));
AirtimeFile.php
class AirtimeFile extends AppModel {
public $useTable = 'cc_files';
public $hasMany = array('AirtimeFileAttribute' => array(
'className' => 'AirtimeFileAttribute',
'foreignKey' => 'track_id'
));
I have a table
Medias:
ID,
PARENT_ID
PATH
TITLE
Items:
ID
medias
Title
description
Models
class ImageGallery extends AppModel {
public $name = 'ImageGallery';
public $useTable = "medias";
var $belongsTo = array(
'Parent' =>
array('className' => 'ImageGallery',
'foreignKey' => 'parent_id',
'dependent' => false,
),
);
var $hasMany = array(
'Children' =>
array('className' => 'ImageGallery',
'foreignKey' => 'parent_id',
'dependent' => false,
),
);
}
class Item extends AppModel {
public $useTable = "items";
public $validate = array(
'title' => array(
'rule' => 'notEmpty'
),
);
public $belongsTo = array(
'Gallery' => array(
'className' => 'ImageGallery',
'foreignKey' => 'medias'),
);
public $hasMany = array(
'ItemMeta' => array( 'className' => 'ItemMeta'),
);
}
The media table contains gallery information. Images are stored in parent_id relation.
When I request Item->find('all'); I am getting only one entry from Media table, but I want to get all its children too. :(
I can get its children by using $this->ImageGallery->findById(10);, but I want to get them with the Item Model.
Use the containable behavior to specify which associated models you want the query to get. http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html
$this->Item->find('all', array('contain' => array('ImageGallery' => array('Children'))));
If you're looking to go deeper than just the child level, like if you want to get the rest of the tree, then you'll need to either write something to recursively fetch the children, the children's children, etc., or perhaps you can alter your code and models to make use of CakePHP's Tree Behavior. See http://book.cakephp.org/2.0/en/core-libraries/behaviors/tree.html and http://www.dereuromark.de/2013/02/17/cakephp-and-tree-structures/
I'm trying to import products from a productfeed and save them into my database. Now I'm having trouble to get this done with cakePHP. It's quite easy to fill the "products" table, but hard to fill the "terms" and joining table "products_terms". I'm using transactions ($dataSource = $this->Supplier->Product->getDataSource();) to import the products, to be sure no incomplete data will be saved into the database. Now cause the terms table uses AutoIncrement for the term id's, it's hard for me to fill the products_terms table cause it's only possible after I know the id of the term. If the ID doesn't exist yet, I cannot know what to fill in the products_terms table. See my ERD below
https://www.dropbox.com/s/pfakd7xplsvr7cc/db_erd.PNG
Does anyone know a solution for this?
Array I send to the database
array(
'Product' => array(
'slug' => 'c24b626d6701d3b07e30b233b989ff8811',
'product_name' => 'DAHLIA 5A',
'price_new' => '159.00',
'affiliate_url' => 'http://ad.zanox.com/ppc/?utm_source=zanox&utm_medium=banner&utm_campaign=product]]',
'product_id_supplier' => 'c24b626d670133d3b07e30b2b989ff8811',
'supplier_id' => 'zizigo_tr',
'feedimport_id' => (int) 1
),
'Image' => array(
(int) 0 => array(
'image' => 'zizigo_tr_5290e6d296084.JPG'
)
),
'Term' => array(
'Term' => array(
(int) 0 => array(
'name' => 'Tommy Hilfiger'
)
)
)
)
And my models:
class Product extends AppModel {
public $name = 'Product';
public $primaryKey = 'product_id';
public $hasMany = array(
'Vote',
'Image'
);
public $hasAndBelongsToMany = array(
'Term' => array(
'className' => 'Term',
'joinTable' => 'products_terms',
'foreignKey' => 'product_id',
'associationForeignKey' => 'term_id',
'unique' => true
)
);
public $belongsTo = array(
'Supplier'
);
}
class Term extends AppModel {
public $name = 'Term';
public $primaryKey = 'term_id';
public $hasAndBelongsToMany = array(
'Product' => array(
'className' => 'Product',
'joinTable' => 'products_terms',
'foreignKey' => 'term_id',
'associationForeignKey' => 'product_id',
'unique' => true
)
);
}
Problem solved. It seems like the Terms table needs to be filled, else cakePHP doesn't know which term to connect to.
So the proper solution is to first check or the Term exists, else insert the Term in the Terms table, and then insert the products using HABTM (using the ID of the Term in the insert array), so the products_terms table will be filled also.
Hi, I am new to cakephp and doing a project on cakephp 2.3.4.
I have to associate product metal class through has many through association . But it doesn't seem to be working.
Model code
class Metal extends AppModel {
public $hasMany = array(
'MetalProduct'
);
}
class Product extends AppModel {
public $hasMany = array(
'MetalProduct'
);
}
App::uses('AppModel', 'Model');
class MetalProduct extends AppModel {
public $belongsTo = array(
'Metal' => array(
'className' => 'Metal',
'foreignKey' => 'metal_id'
),
'Product' => array(
'className' => 'Product',
'foreignKey' => 'product_id'
)
);}
My database table names are metal, products and metal_products
I have multiple select option for selecting more than one metal type.
This is how I get the the list of metals
$metals=$this->Metal->find('list');
$this->set(compact('metals'));
FormHelper code for listbox is
<?php echo $this->Form->input('Metal',array('type' => 'select',
'multiple' => true)); ?>
The product is getting saved successfully but the associations are not.
The debug array give me this
$message = array(
'Product' => array(
'category_id' => '517a514b-0eb0-4ec9-b018-0b948620d4f0',
'name' => 'mangalsutra Diamond',
'slug' => 'mangalsutra_diamond',
'description' => '1212',
'Metal' => array(
(int) 0 => '5183cb65-bf90-459c-b22e-0b748620d4f0',
(int) 1 => '5183ce25-c744-433e-b035-0b748620d4f0'
),
'image' => '121212',
'price' => '12121',
'weight' => '12',
'active' => '1',
'category' => 'Mangalsutra'
)
)
I had put my head through walls but no clue why the associations are not getting saved.
The way they say in tutorials it seems easy, but why its not working?
I have doubts that its not saving because the metal array is passed like this
'Metal' => array(
(int) 0 => '5183cb65-bf90-459c-b22e-0b748620d4f0',
(int) 1 => '5183ce25-c744-433e-b035-0b748620d4f0'
),
It should mention 'id''rather than (int) 0 or something.
Also, my database table for metal_products which I have created manually has
id(primary key)
metal_id(foreign key to Metal.id)
product_id(foreign key to Product.id)
Am I doing something wrong with naming conventions or the way database is created?
Please give me correct ans cause anything I tried from others answer is not working
I am saving it via
$this->Product->saveAll($this->request->data, array('deep' => true))
In your model, is all of that in your MetalProduct Model? If so you need to move
class Metal extends AppModel {
public $hasMany = array(
'MetalProduct'
);
}
to the Metal model and
class Product extends AppModel {
public $hasMany = array(
'MetalProduct'
);
}
to the Product Model
Also add your belongsTo to each Model
I see you have a join table. Join tables are usually for HABTM associations. This is a HasMany. You need to use the other options available to define the foreign key relationships in each model as outlined above.
Please see the examples on the Cake Documentation.
http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html
Also, if you are unsure of how to set up the models correctly, you can always use the Bake feature of Cakephp to generate the models and code for you in that regard.
book.cakephp.org/2.0/en/console-and-shells/code-generation-with-bake.html
If you are more of a visual learner this video tut should help you through the basics
http://www.youtube.com/watch?v=kJAMifqF5s8
The Relation i generated using Bake looks something like this
class Product extends AppModel {
/**
* hasAndBelongsToMany associations
*/
public $hasAndBelongsToMany = array(
'Metal' => array(
'className' => 'Metal',
'joinTable' => 'metals_products',
'foreignKey' => 'product_id',
'associationForeignKey' => 'metal_id',
'unique' => 'keepExisting',
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
));
}
class Metal extends AppModel {
/** hasAndBelongsToMany associations */
public $hasAndBelongsToMany = array(
'Product' => array(
'className' => 'Product',
'joinTable' => 'metals_products',
'foreignKey' => 'metal_id',
'associationForeignKey' => 'product_id',
'unique' => 'keepExisting',
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
));
}
/**
* MetalsProduct Model
* #property Product $Product
* #property Metal $Metal
*/
class MetalsProduct extends AppModel {
/* belongsTo associations */
public $belongsTo = array(
'Product' => array(
'className' => 'Product',
'foreignKey' => 'product_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Metal' => array(
'className' => 'Metal',
'foreignKey' => 'metal_id',
'conditions' => '',
'fields' => '',
'order' => ''
));
}
This worked for me flawlessly.
Hope it works for all.
I've run through the Cakephp documentation on linking models together (creating associations), and I can get a single select dropdown in the scaffolded add and edit views by defining a $hasOne member one model and a corresponding $belongsTo in the other. Now, for a $hasMany relationship, I'm trying to get some kind of form input (either a multi-select dropdown, or checkboxes ... preferably checkboxes) in my scaffolded add and edit views that lets the user choose the associated advertisements for a given rental. But with the below code I get no mention of Advertisements in the Rental add and edit views :'( Am I missing something? Is this even possible? I've seen this done in RoR and Grails but can't get it working with CakePHP. Thanks for any help!
Rental model (app/Model/Rental.php)
<?php
class Rental extends AppModel {
var $name = 'Rental';
var $belongsTo = array(
'Agent' => array(
'className' => 'User',
'foreignKey' => 'agent_id'
),
'Bedroom',
'Landlord' => array(
'className' => 'Landlord',
'foreignKey' => 'landlord_id'
)
);
var $hasMany = array(
'Advertisement' => array(
'className' => 'Advertisement',
'foreignKey' => 'rental_id',
//'conditions' => array('Comment.status' => '1'),
'order' => 'Advertisement.created DESC',
'limit' => '5',
'dependent'=> false
)
);
public $validate = array(
'title' => array(
'rule' => 'notEmpty'
),
'description' => array(
'rule' => 'notEmpty'
)
);
public function isOwnedBy($rental, $user) {
return $this->field('id', array('id' => $rental, 'user_id' => $user)) === $rental;
}
}
Rentals Controller (app/Controller/RentalsController.php)
<?php
class RentalsController extends AppController {
public $scaffold;
}
Advertisement model (app/Model/Advertisement.php)
<?php
class Advertisement extends AppModel {
var $name = 'Advertisement';
var $belongsTo = array(
'Rental' => array(
'className' => 'Rental',
'foreignKey' => 'rental_id'
),
'Author' => array(
'className' => 'User',
'foreignKey' => 'author_id'
)
);
}
Advertisements Controller (app/Controller/AdvertiesementsController.php)
<?php
class AdvertisementsController extends AppController {
public $scaffold;
}
Cake's scaffold views don't show anything from the hasMany side of the relationship. They assume you want to pick it from the model that has the belongsTo.
Frankly, doing it as a multi-select is a little odd for this relationship, IMHO. Multi-selects are normally used for hasAndBelongsToMany relationships. If you want to do it, you'll need to do the work yourself, and not use Cake's scaffolding.