Model HABTM Model Referencing Original Model - php

I have a Team model that is HABTM Match and when I pull the data for a specific Team, I ask for the related Teams for those Matches, but Cake only returns the data for the original Team.
How can I get all the Teams (the opponent) for that Match without looping through the results and pulling them that way?
I am having the same issue with the Team HABTM Player association as well. When I pull a Player, Cake will not return any of the associated Players (teammates) for the linked Team.
My schema:
CREATE TABLE IF NOT EXISTS `matches` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`tournament_id` int(10) unsigned NOT NULL,
`name` varchar(255) NOT NULL DEFAULT '',
`created` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `tournament_id` (`tournament_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 ;
CREATE TABLE IF NOT EXISTS `matches_teams` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`match_id` int(10) unsigned NOT NULL,
`team_id` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `match_id` (`match_id`,`team_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 ;
CREATE TABLE IF NOT EXISTS `players` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 ;
CREATE TABLE IF NOT EXISTS `players_teams` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`player_id` int(10) unsigned NOT NULL,
`team_id` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `player_id` (`player_id`,`team_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 ;
CREATE TABLE IF NOT EXISTS `teams` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`tournament_id` int(10) unsigned NOT NULL,
`name` varchar(255) NOT NULL,
`seed` smallint(2) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `tournament_id` (`tournament_id`),
KEY `seed` (`seed`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 ;
My query:
$this->Team->recursive = 3;
$team = $this->Team->read(null, $id);
$this->set('team', $team);
I have also tried:
$this->Team->contain(array(
'Match' => array(
'Team',
),
));
$team = $this->Team->read(null, $id);
$this->set('team', $team);
The results ($team):
array (size=4)
'Team' =>
array (size=5)
... team data ...
'Match' =>
array (size=2)
0 =>
array (size=9)
... match data ...
'MatchesTeam' =>
array (size=3)
'id' => string '1' (length=1)
'match_id' => string '1' (length=1)
'team_id' => string '1' (length=1)
// there should be an array for 'Team' here
// that contains the opponent team
1 =>
... more match data with same missing 'Team' array ...
... other related models ...

Related

I want to access the inner value of the StdClass object

Array ( [0] => stdClass Object ( [Table] => tbladmin [Create Table] => CREATE TABLE `tbladmin` ( `aid` int(11) NOT NULL AUTO_INCREMENT, `clgcode` varchar(7) NOT NULL, `name` text NOT NULL, `email` text NOT NULL, `mobile` text NOT NULL, `pass` text NOT NULL, `last_noti` int(11) NOT NULL DEFAULT 0, `profilepic` text NOT NULL, PRIMARY KEY (`aid`) ) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=latin1 ) )
How can I access the value of the created table in laravel.
echo $array[0]->{'Create Table'};

MySQL: How can I list sub categories and its items, limiting them to 3?

I would like to retrieve sub categories data and its items, limiting to 3 items per subcategory
sub category 1
item1
item2
item3
sub category 2
item1
item2
item3
sub category 3
item1
item2
item3
here is my table structure
CREATE TABLE `sub_categories` (
`id` INT(11) NOT NULL,
`category_id` INT(11) NULL DEFAULT NULL,
`title` VARCHAR(255) NULL DEFAULT NULL,
`slug` VARCHAR(255) NULL DEFAULT NULL,
`active_start` DATETIME NULL DEFAULT NULL,
`active_end` DATETIME NULL DEFAULT NULL,
`created` DATETIME NULL DEFAULT NULL,
PRIMARY KEY (`id`)
)]]
COLLATE='utf8_general_ci'
ENGINE=InnoDB;
joint table
CREATE TABLE `sub_category_items` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`item_id` INT(11) NOT NULL,
`sub_category_id` INT(11) NOT NULL,
`created` DATETIME NOT NULL,
PRIMARY KEY (`id`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=64;
CREATE TABLE `items` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`title` VARCHAR(255) NULL DEFAULT NULL,
`created` DATETIME NOT NULL,
PRIMARY KEY (`id`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=64;
here is my MySQL query
SELECT `Item`.`title`,`SubCategoryItem`.`id`, `SubCategoryItem`.`item_id`, `SubCategoryItem`.`sub_category_id`, `SubCategoryItem`.`created`, `SubCategory`.`id`, `SubCategory`.`category_id`, `SubCategory`.`title`, `SubCategory`.`active_start`, `SubCategory`.`active_end`, `SubCategory`.`created`
FROM `items` AS `Item`
left JOIN `sub_category_item` AS `SubCategoryItem` ON (`SubCategoryItem`.`item_id`=`Item`.`id`) right JOIN `sub_categories` AS `SubCategory` ON (`SubCategoryItem`.`sub_category_id`=`SubCategory`.`id`)
WHERE `SubCategory`.`category_id` = 21 ORDER BY CASE
WHEN MONTH(`SubCategory`.`active_start`) = MONTH(CURDATE()) THEN 0
WHEN MONTH(`SubCategory`.`active_end`) >= MONTH(CURDATE()) THEN 1
WHEN MONTH(`SubCategory`.`active_start`) <= MONTH(CURDATE()) THEN 2
ELSE 3
END , MONTH(`SubCategory`.`active_start`) desc
what am I doing wrong ?

Database import situation

Mysql doesn't want to add this database into my localhost database section.
Am I doing something wrong?
db.sql
This tutorial: https://github.com/samanz/cakecart
Error:
SQL query:
CREATE TABLE `categories` (
`id` INT( 11 ) NOT NULL AUTO_INCREMENT ,
`name` VARCHAR( 50 ) NULL default NULL ,
`parent_id` INT( 11 ) UNSIGNED default '0',
`order` INT( 3 ) default '0',
`image` VARCHAR( 50 ) NULL default NULL ,
`ids` VARCHAR( 225 ) NULL default NULL ,
`url` VARCHAR( 255 ) NULL default NULL ,
PRIMARY KEY ( `id` ) ,
FOREIGN KEY ( `parent_id` ) REFERENCES categories( `id` ) ,
UNIQUE KEY `url` ( `url` )
);
MySQL said: Documentation
#1005 - Can't create table 'cake_cart.categories' (errno: 150)
Error 150 is a foreign key problem. Likely caused by:
FOREIGN KEY ( `parent_id` ) REFERENCES categories( `id` ) ,
You can't make a "foreign" key reference to the same table you're creating. Simply make the parent_id column indexed instead.
KEY `parent_id` ( `parent_id` ) ,
Should look something like this...
CREATE TABLE IF NOT EXISTS `categories` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`parent_id` int(11) unsigned NOT NULL DEFAULT '0',
`order` int(3) NOT NULL DEFAULT '0',
`img` varchar(50) NOT NULL,
`ids` varchar(255) NOT NULL,
`url` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `url` (`url`),
KEY `parent_id` (`parent_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
I updated the structure and ran it on my database and it worked.

How do I import this database correctly?

How do I import this database correctly?
https://github.com/samanz/cakecart
Every time I import then I get this error:
Error
SQL query:
CREATE TABLE `categories` (
`id` INT( 11 ) NOT NULL AUTO_INCREMENT ,
`name` VARCHAR( 50 ) NULL default NULL ,
`parent_id` INT( 11 ) UNSIGNED default '0',
`order` INT( 3 ) default '0',
`image` VARCHAR( 50 ) NULL default NULL ,
`ids` VARCHAR( 225 ) NULL default NULL ,
`url` VARCHAR( 255 ) NULL default NULL ,
PRIMARY KEY ( `id` ) ,
FOREIGN KEY ( `parent_id` ) REFERENCES categories( `id` ) ,
UNIQUE KEY `url` ( `url` )
);
MySQL said: Documentation
#1005 - Can't create table 'db.categories' (errno: 150)
Foreign key is error 150, but there's much more tables than this error.
Please try import first then answer.
THis one works:
CREATE TABLE `categories` (
`id` INT( 11 ) NOT NULL AUTO_INCREMENT ,
`name` VARCHAR( 50 ) NULL default NULL ,
`parent_id` INT( 11 ),
`order` INT( 3 ) default '0',
`image` VARCHAR( 50 ) NULL default NULL ,
`ids` VARCHAR( 225 ) NULL default NULL ,
`url` VARCHAR( 255 ) NULL default NULL ,
PRIMARY KEY ( `id` ) ,
FOREIGN KEY ( `parent_id` ) REFERENCES categories( `id` ) ,
UNIQUE KEY `url` ( `url` )
);
Edit: Actually you only need to remove unsigned to make it work. But I don't really know why you want it to be default 0. It should be default NULL which is the default default.. :)
I believe the type of parent_id must be the same as id.
The problem is that,You are trying to create table category.In this table you are using forign key but this forign key included table isnot created yet.
So first create the table contaning parent_id .After that try to create the category table
I'm think it's giving you and error because you're referencing an nonexistent, not yet created, table which is the table your trying to create itself.
why don't you create the table first then add the constraint(foreign key)?
but there's a possible error that you may run into whole doing this in the same table. what would happen if the newly added record doesn't have a parent assigned to it? default value is 0. will this give you an error because there's no record with id=0?
I suggest you normalize this by creating a relationship table for this.
CREATE TABLE CategoryGroups //or whatever name you find fits.
(
`Cat_id` int(11) NOT NULL,
`Parent_id` int(11),
FOREIGN KEY (`Parent_id`) REFERENCES categories(`id`),
FOREIGN KEY (`Cat_id`) REFERENCES categories(`id`)
)
Best practices would be to normalize all data and remove all many to many relationships between any two tables by creating relationship tables.

How to merge two tables MySQL-PHP with date sorting

I want to merge the results of two tables in to one.
Please refer the following tables :
Data from microblog table as Row array
Array ( [ID] => 46 [userID] => 1 [userNAME] => user [blog_content] => HAI DEAR [page_name] => honda [page_ID] => 31 [post_time] => 2011-10-18 11:06:54 )
Data from Page_review table as Row array
Array ( [page_review_id] => 5 [page_id] => 31 [page_review_by_id] => 31 [page_review_by_username] => user [page_review_time] => 2011-10-18 11:43:34 [page_review_content] => hai )
Table Microblog MySQL query:
DROP TABLE IF EXISTS `featurezme_store`.`microblog`;
CREATE TABLE `featurezme_store`.`microblog` (
`ID` int(10) unsigned NOT NULL AUTO_INCREMENT,
`userID` int(10) unsigned NOT NULL,
`userNAME` varchar(45) NOT NULL,
`blog_content` text NOT NULL,
`page_name` varchar(45) NOT NULL,
`page_ID` int(10) unsigned NOT NULL,
`post_time` datetime NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=48 DEFAULT CHARSET=latin1;
Table page_review
DROP TABLE IF EXISTS `featurezme_store`.`page_review`;
CREATE TABLE `featurezme_store`.`page_review` (
`page_review_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`page_id` int(10) unsigned NOT NULL,
`page_review_by_id` int(10) unsigned NOT NULL,
`page_review_by_username` varchar(145) NOT NULL,
`page_review_time` datetime NOT NULL,
`page_review_content` varchar(555) NOT NULL,
PRIMARY KEY (`page_review_id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=latin1;
the table Microblog is used to store blog posts & page_review is used to store reviews about pages[Microblog's are in pages].
My requirement is i want to show Blogposts & page reviews sorted by Date [post_time in Microblog & page_review_time on page_review].
how can i do this ? please help me.
Okay I have updated my answear, you can use union as you wanted.
Just have the same number of fields and because the results are placed the one unde the other use fields that make sense to be the one under the other. This example will bring all blogs and reviews created by a specific user (if page_review_by_id actually refers to the user id) and related to a specific page.
(
SELECT
`microblog`.`userID`,
`microblog`.`blog_content` as `blog or review`,
`microblog`.`post_time`,
`microblog`.`page_id`
from `microblog`
where `microblog`.`page_id`='1' and `microblog`.`userID`='1'
)
union
(
SELECT
`page_review`.`page_review_by_id`,
`page_review`.`page_review_content`,
`page_review`.`page_review_time`,
`page_review`.`page_id`
from `page_review`
where `page_review`.`page_id`='1' and `page_review`.`page_review_by_id`='1'
)
======== Edit== Suggesting a schema with foreign keys ================
Because I don't see any foreign keys in your schema if I could suggest optionaly a schema that applies foreign keys I present one below.
These rules are supported by this schema:
Blogs belong to the site and not to the user so there is not on delete cascade applied.
Blogs are created by users and a foreign key is applied so when a user id is inserted the consistency is assured through the foreign key.
The same goes for the page, a foreign key is applid without on delte cascade.
The same goes for the reviews table
If a user or page is deleted no child row is deleted
/********Create ***** ***/
CREATE TABLE user (
user_id int unsigned NOT NULL AUTO_INCREMENT,
username varchar(16) NOT NULL,
userpassword BLOB,
PRIMARY KEY (user_id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE page (
page_id mediumint unsigned NOT NULL AUTO_INCREMENT,
title varchar(55) NOT NULL,
PRIMARY KEY (page_id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE microblog (
blog_id int unsigned NOT NULL AUTO_INCREMENT,
blog_content text NOT NULL,
date_created datetime NOT NULL,
author_id int unsigned NOT NULL,
page_id mediumint unsigned NOT NULL,
PRIMARY KEY (blog_id),
CONSTRAINT blogfk1 FOREIGN KEY (author_id)
REFERENCES user (user_id),
/NO ON DELETE CASCADE/
CONSTRAINT blogfk2 FOREIGN KEY (page_id)
REFERENCES page (page_id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE page_review (
review_id int unsigned NOT NULL AUTO_INCREMENT,
review_content varchar(555) NOT NULL,
date_created datetime NOT NULL,
author_id int unsigned NOT NULL,
page_id mediumint unsigned NOT NULL,
PRIMARY KEY (review_id),
CONSTRAINT reviewfk1 FOREIGN KEY (author_id)
REFERENCES user (user_id),
/NO ON DELETE CASCADE/
CONSTRAINT reviewfk2 FOREIGN KEY (page_id)
REFERENCES page (page_id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/************** ******/
/** ***Insertions*** **/
INSERT INTO user ( username,userpassword)
VALUES ('username11', AES_ENCRYPT('password1',
'encription_key') ),
('username22', AES_ENCRYPT('password2',
'encription_key') );
INSERT INTO page ( title) VALUES
('title1'),('title2');
INSERT INTO microblog (blog_content,date_created,
author_id,page_id) VALUES
('blogcontent1','2011-2-2 12:00','1','1'),
('blogcontent2','2011-2-2 12:00','2','2');
INSERT INTO page_review (review_content,
date_created,author_id,page_id) VALUES
('reviewcontent1','2011-2-2 12:00','1','1'),
('reviewcontent2','2011-2-2 12:00','2','2');
/***** *******/
/******* Queries *** /
/Help on Identifing a user/
SELECT username
FROM user WHERE username ='username22'
and userpassword=AES_ENCRYPT('password2','encription_key')
(
SELECT
microblog.blog_content as blog or content,
microblog.date_created,
microblog.author_id,
microblog.page_id
from microblog
where microblog.author_id='1' and microblog.page_id='1'
)
union
(
SELECT
page_review.review_content,
page_review.date_created,
page_review.author_id,
page_review.page_id
from page_review
where page_review.author_id='1' and page_review.page_id='1'
)

Categories