I have two model user and project and user_id is stored in project table how to implement inner join to display user name for users tables and projects table all fields.
this is my model for defining association
// project model
class Project extends AppModel
{
public $actsAs = array('Containable');
public $hasOne= array('User');
}
//Query Is
$conetent= $this->Project->find('all',array('limit' => 3,'joins' => array(array('table' => 'users','alias' => 'User1','type' => 'INNER','conditions' => array('Project.user_id =User1.id')))));
Best to do using cakephp way.
Add following code to your project controller.
$conetent= $this->Project->find('all',array('limit' => 3,'contain' => array('User')));
What you're looking for is a belongsTo relationship. This way, when you do a find in the Projects controller, it will pull the appropriate record from the User table that the project belongs to. For example (using CakePHP 2.x)
Database Tables
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`firstname` varchar(32) DEFAULT NULL,
`lastname` varchar(32) DEFAULT NULL,
`email` varchar(96) DEFAULT NULL,
`password` varchar(64) DEFAULT NULL,
`created` datetime DEFAULT NULL,
`modified` datetime DEFAULT NULL,
`lastlogin` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
CREATE TABLE `projects` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`name` varchar(32) DEFAULT NULL,
`description` varchar(32) DEFAULT NULL,
`created` datetime DEFAULT NULL,
`modified` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
Users Controller
class UsersController extends AppController {
public $uses = array('User');
public function index() {
}
public function create() {
}
public function update() {
}
public function delete() {
}
}
Users Model
<?php
class User extends AppModel {
}
Projects Controller
<?php
class ProjectsController extends AppController {
public $uses = array('Project');
public $components = array('Paginator', 'RequestHandler');
public function index() {
$projects = $this->Project->find('all'));
}
}
Projects Model
<?php
class Project extends AppModel {
public $useTable = 'projects';
public $belongsTo = array('User');
}
The find in the projects controller should yield a result of something like this:
array(
(int) 0 => array(
'Project' => array(
'id' => '1',
'firstname' => 'John',
'lastname' => 'Smith',
'email' => 'john.smith#domain.tld',
'password' => '******',
'created' => '2016-10-06 00:00:00',
'modified' => '2016-10-06 00:00:00',
'lastlogin' => '2016-10-06 00:00:00'
),
'User' => array(
'id' => '1',
'user_id' => '1',
'name' => 'Project Name',
'description' => 'Project Description',
'created' => '2016-10-06 00:00:00',
'modified' => '2016-10-06 00:00:00'
)
)
)
Related
My app works fine on my local server and I had to change the .htaccess files so that the cakephp app website would work on godaddy hosting, but now I found that some of the information is not retrieved from the tables in the live website which is weird because the same information is retrieved in my local app... I've lost hours searching for the cause but haven't found a possible explanation yet...
On this particular view I should get all the product prices doing this (only works locally):
public function index() {
$options = array('contain' => 'ProductPrice');
$this->set('products', $this->Product->find('all', $options));
}
The necessary models are loaded above in the controller with:
public $uses = array('ProductPrice', 'Product', 'MenuSection');
In the view I should get something like: (which happens in my local app)
products(array)
0(array)
Product(array)
ProductPrice(array)
1(array)
Product(array)
ProductPrice(array)
(and so on...)
but what I'm getting is: (which happens in my live app)
products(array)
0(array)
Product(array)
1(array)
Product(array)
(but no ProductPrice arrays...)
These are the table structures:
CREATE TABLE `product_prices` (
`id` tinyint(9) unsigned NOT NULL AUTO_INCREMENT,
`price` float(6,2) DEFAULT NULL,
`product_id` varchar(36) DEFAULT NULL,
`created` datetime DEFAULT NULL,
`modified` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=181 DEFAULT CHARSET=utf8;
CREATE TABLE `products` (
`id` int(9) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(100) DEFAULT NULL,
`description` text,
`menu_nr` int(9) DEFAULT NULL,
`menu_section_id` int(9) DEFAULT NULL,
`created` datetime DEFAULT NULL,
`modified` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=96 DEFAULT CHARSET=utf8;
These are the models:
class ProductPrice extends AppModel {
public $name = 'ProductPrice';
public $useTable = 'product_prices';
public $actsAs = array('Containable');
public $belongsTo = array('Product');
public $validate = array(
'product_id' => array(
'rule' => 'notEmpty',
'message' => 'This is a required field and cannot be left empty.'
),
);
}
class Product extends AppModel {
public $name = 'Product';
public $actsAs = array('Containable');
public $hasMany = array(
'ProductPrice'
);
public $belongsTo = array(
'MenuSection',
);
public $validate = array(
'name' => array(
'rule-empty' => array(
'rule' => 'notEmpty',
'message' => 'This is a required field and cannot be left empty.'
),
'rule-unique' => array(
'rule' => 'isUnique',
'message' => 'There is already one product with that name.'
),
),
'menu_nr' => array(
'rule' => 'isUnique',
'message' => 'There is already one product with that Menu Item Nr.'
)
);
}
Could anyone help me find the cause? Where else should I look for? Any hints or best practice suggestions are welcome too, thanks.
I finally figured it out... it was a simple problem after all. My model names were called 'product.php' and 'product_prices.php' because I followed CakePHP blog example, but once I changed them to 'Product.php' and 'ProductPrice.php' (and so on...) they started working! I thought about this before but forgot to actually try it while in the middle of the mess. Cheers for trying to help
I have 4 models, Application , Accommodation, Invoice and InvoiceItem
Invoice, Application and Accommodation all hasMany InvoiceItem
InvoiceItem belongsTo Application, Accommodation and Invoice
The purpose of all this is so that the line items of an Invoice (which come from the InvoiceItem model) can be associated with either an Accommodation or an Application via the InvoiceItem.foreign_id depending on whether InvoiceItem.class is set as Accommodation or Application
From what I understand, this is known as a polymorphic association.
Unfortunately, when I do a $this->Invoice->find('all'); with recursive set to 3 (or set to -1 with the appropriate fields specified in containable) I get this MySQL error:
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column
'InvoiceItem.class' in 'where clause'
SQL Query: SELECT `Application`.`id`, `Application`.`name`,
`Application`.`created`, `Application`.`updated` FROM `applications`
AS `Application` WHERE `Application`.`id` = 3786 AND
`InvoiceItem`.`class` = 'Application'
I don't understand why Cake would try to add InvoiceItem.class as a condition without creating a join for the InvoiceItem model.
Here are my models (note: I've cut down the number of fields in each for readability -- the Application and Accommodation models share very few similar fields in reality):
Accommodation Model
CREATE TABLE `accommodations` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`created` datetime NOT NULL,
`updated` datetime NOT NULL,
PRIMARY KEY (`id`)
)
<?php
class Accommodation extends AppModel {
var $name = 'Accommodation';
var $hasMany = array(
'InvoiceItem' => array(
'className' => 'InvoiceItem',
'foreignKey' => 'foreign_id',
'conditions' => array('InvoiceItem.class' => 'Accommodation'),
)
);
}
?>
Application Model
CREATE TABLE `applications` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`created` datetime NOT NULL,
`updated` datetime NOT NULL,
PRIMARY KEY (`id`)
)
<?php
class Application extends AppModel {
var $name = 'Application';
var $hasMany = array(
'InvoiceItem' => array(
'className' => 'InvoiceItem',
'foreignKey' => 'foreign_id',
'conditions' => array('InvoiceItem.class' => 'Application'),
)
);
}
?>
InvoiceItem model
CREATE TABLE IF NOT EXISTS `invoice_items` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`class` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`foreign_id` int(10) unsigned NOT NULL,
`name` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`created` datetime DEFAULT NULL,
`modified` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
)
<?php
class InvoiceItem extends AppModel {
var $name = 'InvoiceItem';
var $belongsTo = array(
'Accommodation' => array(
'foreignKey' => 'foreign_id',
'conditions' => array('InvoiceItem.class' => 'Accommodation')
),
'Application' => array(
'foreignKey' => 'foreign_id',
'conditions' => array('InvoiceItem.class' => 'Application')
),
'Invoice' => array(
'className' => 'Invoice',
'foreignKey' => 'invoice_id',
),
);
}
?>
Invoice model
CREATE TABLE IF NOT EXISTS `invoices` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`created` datetime DEFAULT NULL,
`modified` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
)
<?php
class Invoice extends AppModel {
var $name = 'Invoice';
var $hasMany = array(
'InvoiceItem' => array(
'className' => 'InvoiceItem'
'foreignKey' => 'invoice_id'
)
);
}
?>
I'm using CakePHP 2.4.0
class InvoiceItem extends AppModel,and bind no belongsTo.
class InvoiceItem extends AppModel {
var $name = 'InvoiceItem';
var $belongsTo = null
}
you can assign a suitable belongsTo to InvoiceItem at Runtime depend on content
$this->InvoiceItem->bindModel(array( 'belongsTo' => array(...) ) )
I have 3 models, Page , Course and Content
Page and Course contain meta data and Content contains HTML content.
Page and Course both hasMany Content
Content belongsTo Page and Course
To avoid having page_id and course_id fields in Content (because I want this to scale to more than just 2 models) I am looking at using Polymorphic Associations. I started by using the Polymorphic Behavior in the Bakery but it is generating waaay too many SQL queries for my liking and it's also throwing an "Illegal Offset" error which I don't know how to fix (it was written in 2008 and nobody seems to have referred to it recently so perhaps the error is due to it not having been designed for Cake 2?)
Anyway, I've found that I can almost do everything I need by hardcoding the associations in the models as such:
Page Model
CREATE TABLE `pages` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`slug` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`created` datetime NOT NULL,
`updated` datetime NOT NULL,
PRIMARY KEY (`id`)
)
<?php
class Page extends AppModel {
var $name = 'Page';
var $hasMany = array(
'Content' => array(
'className' => 'Content',
'foreignKey' => 'foreign_id',
'conditions' => array('Content.class' => 'Page'),
)
);
}
?>
Course Model
CREATE TABLE `courses` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`slug` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`created` datetime NOT NULL,
`updated` datetime NOT NULL,
PRIMARY KEY (`id`)
)
<?php
class Course extends AppModel {
var $name = 'Course';
var $hasMany = array(
'Content' => array(
'className' => 'Content',
'foreignKey' => 'foreign_id',
'conditions' => array('Content.class' => 'Course'),
)
);
}
?>
Content model
CREATE TABLE IF NOT EXISTS `contents` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`class` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`foreign_id` int(11) unsigned NOT NULL,
`title` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`content` text COLLATE utf8_unicode_ci NOT NULL,
`created` datetime DEFAULT NULL,
`modified` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
)
<?php
class Content extends AppModel {
var $name = 'Content';
var $belongsTo = array(
'Page' => array(
'foreignKey' => 'foreign_id',
'conditions' => array('Content.class' => 'Page')
),
'Course' => array(
'foreignKey' => 'foreign_id',
'conditions' => array('Content.class' => 'Course')
)
);
}
?>
The good thing is that $this->Content->find('first') only generates a single SQL query instead of 3 (as was the case with the Polymorphic Behavior) but the problem is that the dataset returned includes both of the belongsTo models, whereas it should only really return the one that exists. Here's how the returned data looks:
array(
'Content' => array(
'id' => '1',
'class' => 'Course',
'foreign_id' => '1',
'title' => 'something about this course',
'content' => 'The content here',
'created' => null,
'modified' => null
),
'Page' => array(
'id' => null,
'title' => null,
'slug' => null,
'created' => null,
'updated' => null
),
'Course' => array(
'id' => '1',
'title' => 'Course name',
'slug' => 'name-of-the-course',
'created' => '2012-10-11 00:00:00',
'updated' => '2012-10-11 00:00:00'
)
)
I only want it to return one of either Page or Course depending on which one is specified in Content.class
UPDATE: Combining the Page and Course models would seem like the obvious solution to this problem but the schemas I have shown above are just shown for the purpose of this question. The actual schemas are actually very different in terms of their fields and the each have a different number of associations with other models too.
UPDATE 2
Here is the query that results from running $this->Content->find('first'); :
SELECT `Content`.`id`, `Content`.`class`, `Content`.`foreign_id`, `Content`.`title`,
`Content`.`slug`, `Content`.`content`, `Content`.`created`, `Content`.`modified`,
`Page`.`id`, `Page`.`title`, `Page`.`slug`, `Page`.`created`, `Page`.`updated`,
`Course`.`id`, `Course`.`title`, `Course`.`slug`, `Course`.`created`,
`Course`.`updated` FROM `cakedb`.`contents` AS `Content`
LEFT JOIN `cakedb`.`pages` AS `Page` ON
(`Content`.`foreign_id` = `Page`.`id` AND `Content`.`class` = 'Page')
LEFT JOIN `cakedb`.`courses` AS `Course` ON (`Content`.`foreign_id` = `Course`.`id`
AND `Content`.`class` = 'Course') WHERE 1 = 1 LIMIT 1
Your query is okay. Just filter empty values after find:
public function afterFind($results, $primary = false) {
return Set::filter($results, true);
}
Take a look at this question.
You can also try to provide conditions to you find query that Page.id in not null or Course.id is not null.
I have the table 'posts' and 'users':
CREATE TABLE `posts` (
`id` int(11) unsigned NOT NULL auto_increment,
`name` varchar(255) default NULL,
`date` datetime default NULL,
`content` text,
`user_id` int(11) default NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE `users` (
`id` int(11) unsigned NOT NULL auto_increment,
`name` varchar(100) default NULL,
`email` varchar(150) default NULL,
`firstname` varchar(60) default NULL,
`lastname` varchar(60) default NULL,
PRIMARY KEY (`id`)
);
and the model classes:
<?php
class Post extends AppModel {
var $name = 'Post';
var $belongsTo = array(
'User'=>array(
'className'=>'User',
'foreignKey'=>'user_id',
'conditions'=>null,
'fields'=>null)
);
}
?>
<?php
class User extends AppModel {
var $name = 'User';
var $hasMany = array('Post');
}
?>
I am testing using var $scaffold.
However, after I add some users, I can only see an empty select menu in the add post page, which means the association is not working. I don't know what is wrong with my code. Please help me out.
Thank you very much!
See the below url:-
http://book.cakephp.org/1.3/view/1042/belongsTo
http://book.cakephp.org/1.3/view/1043/hasMany
//Try This for belongsTo :--
<?php
class Profile extends AppModel {
var $name = 'Profile';
var $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id'
)
);
}
?>
Or
<?php
class Profile extends AppModel {
var $name = 'Profile';
var $belongsTo = array('User');
}
?>
//For hasmany
<?php
class User extends AppModel {
var $name = 'User';
var $hasMany = array(
'Post' => array(
'className' => 'Post',
'foreignKey' => 'user_id',
'conditions' => array('Comment.status' => '1'),
'order' => 'Comment.created DESC',
'limit' => '5',
'dependent'=> true
)
);
}
?>
I am starting with cakePHP too and banged my head for some time before I realized that the "conventions" require the Model filename to be CamelCased.
Try using:
Post.php
User.php
otherwise it seems the models are totally ignored.
Three simple tables...
Feedname (eg. News or
Events) which are the names of RSS
feeds.
Posts that belong to a
Feedname
User, that owns all
the posts
I want to use the Form helper to automatically give me a select box so that when I add a post I can select which Feedname to assign it to.
It seems like posts belong to both Feedname and User but I can't get the correct combination of belongsTo and hasMany in my model/ .php files. The select box for feedname is shown, but there is nothing in it. Can anyone point me in the right direction?
The tables look like this at the moment:
CREATE TABLE `feednames` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`created` datetime DEFAULT NULL,
`modified` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `posts` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`body` text COLLATE utf8_unicode_ci,
`created` datetime DEFAULT NULL,
`modified` datetime DEFAULT NULL,
`user_id` int(10) unsigned NOT NULL DEFAULT '1',
`feedname_id` int(10) unsigned NOT NULL DEFAULT '1',
PRIMARY KEY (`id`),
KEY `foreign_key` (`user_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) CHARACTER SET latin1 NOT NULL,
`password` char(40) CHARACTER SET latin1 NOT NULL,
`group_id` int(11) NOT NULL,
`created` datetime DEFAULT NULL,
`modified` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
edit - adding the model .php files ...
class Feedname extends AppModel {
var $name = 'Feedname';
var $hasMany = array(
'Post' => array(
'className' => 'Post',
'foreignKey' => 'feedname_id',
'dependent' => false
)
);
}
class Post extends AppModel {
var $name = 'Post';
var $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id'
),
'Feedname' => array(
'foreignKey' => 'feedname_id'
)
);
}
class User extends AppModel {
var $name = 'User';
var $hasMany = array(
'Post' => array(
'className' => 'Post',
'foreignKey' => 'user_id',
'dependent' => false
)
);
}
edit - adding SQL dump ** ...
/posts/index.ctp:
SELECT COUNT(*) AS count FROM posts AS Post LEFT JOIN users AS User ON (Post.user_id = User.id) LEFT JOIN feednames AS Feedname ON (Post.feedname_id = Feedname.id) WHERE 1 = 1
SELECT Post.id, Post.title, Post.body, Post.created, Post.modified, Post.user_id, Post.feedname_id, User.id, User.username, User.password, User.group_id, User.created, User.modified, Feedname.id, Feedname.name, Feedname.created, Feedname.modified FROM posts AS Post LEFT JOIN users AS User ON (Post.user_id = User.id) LEFT JOIN feednames AS Feedname ON (Post.feedname_id = Feedname.id) WHERE 1 = 1 ORDER BY Post.created DESC LIMIT 10
Please note: /posts/add.ctp does not produce any SQL dump, so it's not getting the select box options from the database, this is what I'm trying to fix with proper model relationships.
Do you have something like this in your controller methods (e.g. the admin_add / admin_edit functions)?
$feednames = $this->Feedname->find('list');
$this->set('feednames', $feednames);
Cake should then automatically populate the select list with these values. Or you can manually set the values with:
$form->input('feedname_id', array('options' => $feednames));
So far I agree :
posts > belongs to > users
posts > belongs to > feednames
feednames > has many > posts
users > has many > posts
Just checking, but, did you actually insert data into your tables? Else it would be only logical that your select field is empty. Also, what does the Sql dump say in debug mode?