cakephp how to do a nested join - php

i have the following cakephp binding relationships set up. they are working find but how to get the comment's user record nested in the results as well?
$this->Posts->bindModel(array(
'hasOne' => array(
'User' => array(
'foreignKey' => false,
'type' => 'INNER',
'conditions' => array('Posts.user_id = Users.id')
)
),
'hasMany' => array(
'Comment' => array(
'foreignKey' => 'post_id',
'conditions' => array('Comment.active' => 1)
)
)
));
this works great to get a result like:
[1] => Array
(
[Posts] => Array
(
[title] => test post
[body] => test body
[published] =>
[id] => 15
)
[User] => Array
(
[id] => 7
[username] => admin
[password] => d0557b9de8bb6f7fb3248a017c7b67a6
[email] => frankhinchey#gmail.com
[group_id] => 1
[created] => 2011-11-21 15:19:09
)
[Comment] => Array
(
[0] => Array
(
[id] => 10
[user_id] => 7
[post_id] => 15
[text] => testdfasdfdsfasdfasdfasdfasd
[active] => 1
[created] => 2011-12-02 20:50:57
[published] => 2011-12-05 13:58:25
)
[1] => Array
(
[id] => 11
[user_id] => 7
[post_id] => 15
[text] => this is a test comment
[active] => 1
[created] => 2011-12-02 21:31:56
[published] => 2011-12-03 11:34:32
)
)
)
)
my question is how to also get the related user on the comment? is there a way to nest in my query the hasone relationship between comment and user?

Can you use contain within bindModel? Never tried it before... but worth a shot.
$this->Posts->bindModel(array(
'hasOne' => array(
'User' => array(
'foreignKey' => false,
'type' => 'INNER',
'conditions' => array('Posts.user_id = Users.id')
)
),
'hasMany' => array(
'Comment' => array(
'foreignKey' => 'post_id',
'conditions' => array('Comment.active' => 1),
'contain' => array('User'),
)
)
));
Actually now that I read your question I'm not really sure what you want. Do you want the user_id of who the person is commenting on? Can you just elaborate on what you want a little more.

Related

Insert column value into array using array mapping

I have two array i want to add another key in first array by matching keys with second array below are the array that I have
First Array
Array
(
[0] => Array
(
[id] => 5198
[full_name] => Afnan
[username] => eay.d
[is_live] => 1
[picture] => http://d2flqmogg10inr.cloudfront.net/assets/profile_images/thumbnail1/haulysu4upload-image1499828198.jpg
)
[1] => Array
(
[id] => 5213
[full_name] => Nouf
[username] => noufalswailem
[is_live] => 1
[picture] => http://d2flqmogg10inr.cloudfront.net/assets/profile_images/thumbnail1/jlyfgi4fupload-image1502276119.jpg
)
[2] => Array
(
[id] => 5218
[full_name] => Mohammed Bin Abdullah
[username] => almuribadh
[is_live] => 1
[picture] => http://d2flqmogg10inr.cloudfront.net/assets/profile_images/thumbnail1/m3ttx0luupload-image1500789921.jpg
)
[3] => Array
(
[id] => 5225
[full_name] => Shadin Alshobaily
[username] => shash
[is_live] => 1
[picture] => http://d2flqmogg10inr.cloudfront.net/assets/profile_images/thumbnail1/srura4raupload-image1499829155.jpg
)
[4] => Array
(
[id] => 5251
[full_name] => Razan
[username] => ra.m
[is_live] => 1
[picture] => http://d2flqmogg10inr.cloudfront.net/assets/profile_images/thumbnail1/rwfieka6upload-image1499831173.jpg
)
)
Second array that I have
Array
(
[0] => Array
(
[id] => 38395
[request_by] => 2
[request_to] => 5198
)
)
Required Result
Array
(
[0] => Array
(
[id] => 5198
[full_name] => Afnan
[username] => eay.d
[is_live] => 1
[request_sent] => true
[picture] => http://d2flqmogg10inr.cloudfront.net/assets/profile_images/thumbnail1/haulysu4upload-image1499828198.jpg
)
[1] => Array
(
[id] => 5213
[full_name] => Nouf
[username] => noufalswailem
[is_live] => 1
[request_sent] => false
[picture] => http://d2flqmogg10inr.cloudfront.net/assets/profile_images/thumbnail1/jlyfgi4fupload-image1502276119.jpg
)
[2] => Array
(
[id] => 5218
[full_name] => Mohammed Bin Abdullah
[username] => almuribadh
[is_live] => 1
[request_sent] => false
[picture] => http://d2flqmogg10inr.cloudfront.net/assets/profile_images/thumbnail1/m3ttx0luupload-image1500789921.jpg
)
[3] => Array
(
[id] => 5225
[full_name] => Shadin Alshobaily
[username] => shash
[is_live] => 1
[request_sent] => false
[picture] => http://d2flqmogg10inr.cloudfront.net/assets/profile_images/thumbnail1/srura4raupload-image1499829155.jpg
)
[4] => Array
(
[id] => 5251
[full_name] => Razan
[username] => ra.m
[is_live] => 1
[request_sent] => false
[picture] => http://d2flqmogg10inr.cloudfront.net/assets/profile_images/thumbnail1/rwfieka6upload-image1499831173.jpg
)
)
This is just because requested by user 2 has sent friend request to 5198 user and so that's why request_sent is true to this user only and remining will false if friend request not send I want to do this work using buitl in function instead of loops.
Seems very easy, may be i don't uderstand the task
$tmp = array_flip(array_column($arr2, 'request_to'));
foreach($arr1 as &$v) {
$v['request_sent'] = isset($tmp[$v[id]]);
}
Solution without loops (check it online):
<?php
$users = [
['id' => 5198, 'full_name' => 'Afnan', 'username' => 'eay.d', 'is_live' => 1, 'picture' => 'someurl'],
['id' => 5213, 'full_name' => 'Nouf', 'username' => 'noufalswailem', 'is_live' => 1, 'picture' => 'someurl'],
['id' => 5218, 'full_name' => 'Mohammed Bin Abdullah', 'username' => 'almuribadh', 'is_live' => 1, 'picture' => 'someurl'],
['id' => 5225, 'full_name' => 'Shadin Alshobaily', 'username' => 'shash', 'is_live' => 1, 'picture' => 'someurl'],
['id' => 5251, 'full_name' => 'Razan', 'username' => 'ra.m', 'is_live' => 1, 'picture' => 'someurl'],
];
$requests = [
['request_by' => 2, 'request_to' => 5198],
['request_by' => 2, 'request_to' => 5225],
];
array_walk($users, 'setUserRequestStatus', array_column($requests, 'request_to'));
var_dump($users);
function setUserRequestStatus(&$user, $key, $requests_sent_to) {
$user['request_sent'] = in_array($user['id'], $requests_sent_to);
}

Building new multidimensional array from multidimensional array and interpreting the order

I have been trying to get this to work for 7.5 hours now, but my brain has melted.
I have a multi-dimensional array similar to the following:
array (
'expanded' => true,
'key' => 'root_1',
'title' => 'root',
'children' =>
array (
0 =>
array (
'folder' => false,
'key' => '_1',
'title' => 'News',
'data' =>
array (
'id' => '3',
'parent_id' => 0,
),
),
1 =>
array (
'folder' => true,
'key' => '_2',
'title' => 'Animations',
'data' =>
array (
'id' => '5',
'parent_id' => '0',
),
'children' =>
array (
0 =>
array (
'folder' => false,
'key' => '_3',
'title' => 'The Simpsons',
'data' =>
array (
'id' => '1',
'parent_id' => '5',
),
),
1 =>
array (
'folder' => false,
'key' => '_4',
'title' => 'Futurama',
'data' =>
array (
'id' => '4',
'parent_id' => '5',
),
),
),
),
2 =>
array (
'folder' => true,
'key' => '_5',
'title' => 'Episodes',
'data' =>
array (
'id' => '6',
'parent_id' => '0',
),
'children' =>
array (
0 =>
array (
'folder' => true,
'key' => '_6',
'title' => 'UK Episodes',
'data' =>
array (
'id' => '7',
'parent_id' => '6',
),
'children' =>
array (
0 =>
array (
'folder' => false,
'key' => '_7',
'title' => 'Coupling',
'data' =>
array (
'id' => '2',
'parent_id' => '7',
),
),
),
),
1 =>
array (
'folder' => true,
'key' => '_8',
'title' => 'AU Episodes',
'data' =>
array (
'id' => '8',
'parent_id' => '6',
),
),
),
),
),
)
I need to search through all sub arrays and build a new array returning the id and parent_id of each of the children AND also interpreting the order of the array children from the order they appear in the original array.
The output needs to end up something like this:
Array
(
[0] => Array
(
[id] => 1
[parent_id] => 5
[order] => 1
)
[1] => Array
(
[id] => 2
[parent_id] => 7
[order] => 1
)
[2] => Array
(
[id] => 4
[parent_id] => 5
[order] => 2
)
)
I have tried the following recursion approach, however my code turns into a mess, and thats before I've even attempted to set the order of the items.
I've also searched stackoverflow for another example that I could learn from however I haven't found anything yet... If there is another example please feel free to point me in the right direction.
I appreciate any assistance!
A super easy way to run over all these is to use a RecursiveIteratorIterator over a RecursiveArrayIterator, pluck out the 'data' arrays and add in the key as the order.
Example:
$array = []; // Your starting array with stuff in it.
$iterator = new RecursiveIteratorIterator(
new RecursiveArrayIterator($array),
RecursiveIteratorIterator::SELF_FIRST
);
$result = [];
foreach ($iterator as $key => $value) {
if (isset($value['data'])) {
$result[] = array_merge($value['data'], ['order' => $key]);
}
}
print_r($result);
Output:
Array
(
[0] => Array
(
[id] => 3
[parent_id] => 0
[order] => 0
)
[1] => Array
(
[id] => 5
[parent_id] => 0
[order] => 1
)
[2] => Array
(
[id] => 1
[parent_id] => 5
[order] => 0
)
[3] => Array
(
[id] => 4
[parent_id] => 5
[order] => 1
)
[4] => Array
(
[id] => 6
[parent_id] => 0
[order] => 2
)
[5] => Array
(
[id] => 7
[parent_id] => 6
[order] => 0
)
[6] => Array
(
[id] => 2
[parent_id] => 7
[order] => 0
)
[7] => Array
(
[id] => 8
[parent_id] => 6
[order] => 1
)
)
If i'm understanding what you're trying to do properly the below snippet should work. No need for the order key in each sub array because that's recorded automatically when you insert a new array index into $new_array.
Let me know if you have any other problems.
$new_array = [];
$current_array = []; //with all your data
foreach($current_array['children'] as $array) {
$new_array[] = [
'id' => $array['data']['id'],
'parent_id' => $array['data']['parent_id'];
];
}

PHP Webservice not being consumed

I'm using nusoap and I'm having dificulties in consuming webservices ... What I'm trying to return to who consumes one of the webservices is one array ...
In the first case I'm trying to return this type of array:
Array ( [0] => EX123EX [1] => Test [2] => 2013/04/27 [3] => 12:06 [4] => This is a test [5] => [+]Info [-]Info )
And for this I have:
$server->wsdl->addComplexType(
'details',
'complexType',
'struct',
'all',
'',
array(
'id' => array('name' => 'id', 'type' => 'xsd:string'),
'product' => array('name' => 'product', 'type' => 'xsd:string'),
'date' => array('name' => 'date', 'type' => 'xsd:string'),
'hour' => array('name' => 'hour', 'type' => 'xsd:string'),
'status' => array('name' => 'status', 'type' => 'xsd:string'),
'info' => array('name' => 'info', 'type' => 'xsd:string'),
));
$server->register(
'getdetails',
array('url' => 'xsd:string'),
array('return' => 'tns:details'),
$namespace,
false,
'rpc',
'literal',
'details');
and the function:
function getdetalhes($url)
{
$details = getHeader($url);
return $details;
}
The problem is that the webservice is not consumed... When I make the request I have no answer ... With this error I'm also unable to keep on for the next webservice that would return an array with this structure:
Array ( [0] => Array ( ) [1] => Array ( [0] => 2012/12/13 [1] => 12:06 [2] => Test [3] => - [4] => Test Test [5] => Test ) [2] => Array ( [0] => 2012/12/13 [1] => 09:23 [2] => Test Test [3] => - [4] => Test [5] => - ) [3] => Array ( [0] => 2012/12/12 [1] => 17:43 [2] => Test [3] => - [4] => Test [5] => - ) [4] => Array ( [0] => 2012/12/12 [1] => 11:25 [2] => Test [3] => Test [4] =>Test [5] => - )
I'm confident that the error is on the complexType declaration but I can't figure out what is the problem, can someone help me please?
Please, make sure that you're accessing the associative keywords of the array instead of indexes :)

Cakephp Containable not attaching to Model

I've used containable tons of times but I can not figure for the life of me why it is not working correctly now.
I have 3 models Project, User, and Country.
Project belongsTo User
User belongsTo Country
here is the models
<?php
class User extends AppModel {
var $name = 'User';
var $displayField = 'username';
var $belongsTo = array(
'AccountType' => array(
'className' => 'AccountType',
'foreignKey' => 'account_type_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'PaymentType' => array(
'className' => 'PaymentType',
'foreignKey' => 'payment_type_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Country' => array(
'className' => 'Country',
'foreignKey' => 'country_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'InstantMessenger' => array(
'className' => 'InstantMessenger',
'foreignKey' => 'instant_messenger_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'TimeZone' => array(
'className' => 'TimeZone',
'foreignKey' => 'time_zone_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Language' => array(
'className' => 'Language',
'foreignKey' => 'language_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
);
}
<?php
class Project extends AppModel {
var $name = 'Project';
var $displayField = 'project_title';
var $belongsTo = array(
'User' => array(
'className' => 'User',
'conditions' => '',
'fields' => '',
'order' => ''
),
'ProjectType' => array(
'className' => 'ProjectType',
'foreignKey' => 'project_type_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'BiddingType' => array(
'className' => 'BiddingType',
'foreignKey' => 'bidding_type_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'ProjectStatus' => array(
'className' => 'ProjectStatus',
'foreignKey' => 'project_status',
'conditions' => '',
'fields' => '',
'order' => ''
),
'EmploymentType',
'PaymentType',
);
}
<?php
class Country extends AppModel {
var $name = 'Country';
var $displayField = 'name';
//The Associations below have been created with all possible keys, those that are not needed can be removed
var $hasMany = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'country_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
}
Now here is my code to try to attach a containable to project and add User
function smartbid($projid = null)
{
$this->loadModel('Content');
$content = $this->Content->findById(53);
$this->set('content',$content);
$this->Project->Behaviors->attach('Containable');
$options = array(
array(
'conditions'=>
array(
'Project.id'=>$projid
),
'contain'=>array(
'User'
)
)
);
$project = $this->Project->find('first',$options);
$this->set(compact('project'));
}
Now cakephp completely ignores my attached containable and returns this
Array
(
[Project] => Array
(
[id] => 1
[user_id] => 19
[status] => 1
[approval_status] => 0
[project_status] => 2
[approval_date] => 2012-02-08
[project_title] => Database Reporting
[project_type_id] => 3
[budget] => 100
[bidding_type_id] => 1
[employment_type_id] =>
[esl] => 1
[secrecy] => 0
[secrecy_file] =>
[description] => this is my first post
[performance] => 0
[cme] => 6
[files_share] =>
[project_deadline] => 1
[delivery_deadline] => 2012-02-16
[bidding_deadline] => 2012-02-27
[legal] => 1)Employer requires complete and fully-functional working program(s) in executable form as well as complete source code of all work done.
2) Deliverables must be in ready-to-run condition as follows (depending on the nature of the project and deliverables):
2a) If there are any server-side deliverables (intended to only exist in one place in the Employer's environment) then they must be installed by the Consultant in ready-to-run condition (unless specified elsewhere by the Employer).
2b) All other software (including but not limited to any desktop software or software the Employer intends to distribute) must include a software installation package that will install the software in ready-to-run condition on the platform(s) specified in this project (unless specified elsewhere by the Employer).
3) All deliverables will be considered 'work made for hire' under U.S. Copyright law. Employer will receive exclusive and complete copyrights to all work purchased.
3b) No part of the deliverable may contain any copyright restricted 3rd party components (including GPL, GNU, Copyleft, etc.) unless all copyright ramifications are explained AND AGREED TO by the Employer on the site per the Consultant's Worker Legal Agreement.
[close] => 0
[agree] => 1
[created] => 2012-02-06
[top_employer] => 0
[view_counter] => 41
[pause_deadline] =>
[payment_type_id] =>
[status_date] => 2012-02-08
[redeem_amount] =>
)
[User] => Array
(
[id] => 19
[username] => EmployerWarrior
[password] => 7c6a180b36896a0a8c02787eeafb0e4c
[account_type_id] => 2
[picture] =>
[resume] =>
[alert] => 0
[first_name] => Yaser
[last_name] => Ibrahim
[company_name] => Software Momentum, Inc.
[company_incorporated] => 1
[email] => Yaser43082#hotmail.com
[address] => 123 Somewhere
[apartment_number] =>
[city] => Some City
[state] => Some State
[country_id] => 1
[phone_city] => 614
[phone_country] => 1
[phone_number] => 1234567
[fax_city] =>
[fax_country] =>
[fax_number] =>
[instant_messenger_id] => 1
[im_username] =>
[postal] => 43082
[time_zone_id] => 58
[language_id] => 23
[esl] => 1
[us_resident] => 1
[ssnum] => 1234567
[have_ssnum] => 1
[work_permit_number] =>
[alien_number] =>
[work_permit] => 0
[green_card] => 0
[payment_type_id] => 4
[ip] =>
[created] => 2012-02-06 08:59:35
[top_employer] => 0
[top_consultant] => 0
[rating] => 0
[money_invested] => 0
[money_earned] => 0
[total_projects] => 0
[last_project] => 0
[active] => 0
[last_login] =>
[last_seen] => 2012-02-26 14:13:08
[is_logged_in] => 0
[time_format_id] => 1
[paypal_id] =>
[momentum_expiration] => 2014-02-12
)
[ProjectType] => Array
(
[id] => 3
[name] => Small Business Project
[min] => 100
[max] => 499.99
[order] => 4
)
[BiddingType] => Array
(
[id] => 1
[type] => Public Bidding
[percentage] => 15
)
[ProjectStatus] => Array
(
[id] => 2
[status] => Open Bids
[icon] => bid_open.png
[order] => 3
[default] => 1
)
[EmploymentType] => Array
(
[id] =>
[name] =>
)
[PaymentType] => Array
(
[id] =>
[type] =>
)
[BidWinner] => Array
(
[id] =>
[comment_group_id] =>
[bid_status_id] =>
[project_id] =>
[bid_amount] =>
[performance_guaranteed] =>
[isSmartBid] =>
[user_id] =>
[comment] =>
[isWinner] =>
[created] =>
)
[HRWWallet] => Array
(
[id] => 1
[user_id] => 19
[account_type_id] =>
[entity_id] => 1
[payment_type_id] => 1
[entity_type_id] => 1
[payment_to_id] =>
[payment_status_id] => 2
[payment_number] =>
[service_id] => 4
[created] => 2012-02-10 18:45:52
[description] => Payment for project
[credit] => 100
[debit] => 0
[billing_address] =>
[billing_city] =>
[billing_state] =>
[billing_country_id] =>
[billing_name] =>
[exp_year] =>
[exp_month] =>
[security_code] =>
)
[EmployerRating] => Array
(
[id] =>
[owner_id] =>
[project_id] =>
[rated_by_id] =>
[account_type_id] =>
[rating] =>
[comment] =>
[created] =>
)
[ConsultantRating] => Array
(
[id] =>
[owner_id] =>
[project_id] =>
[rated_by_id] =>
[account_type_id] =>
[rating] =>
[comment] =>
[created] =>
)
[Arbitration] => Array
(
[id] => 5
[project_id] => 1
[defendent_id] => 20
[plantiff_id] => 19
[magistrate_id] => 17
[arbitration_status_id] => 1
[verdict] =>
[created] => 2012-02-11 23:00:00
[end] =>
)
[Bid] => Array
(
[0] => Array
(
[id] => 1
[comment_group_id] => 1
[bid_status_id] => 1
[project_id] => 1
[bid_amount] => 75
[performance_guaranteed] => 10
[isSmartBid] => 0
[user_id] => 20
[comment] => Hi, I was writing about your project
[isWinner] =>
[created] => 2012-02-08 14:15:04
)
[1] => Array
(
[id] => 2
[comment_group_id] => 2
[bid_status_id] => 1
[project_id] => 1
[bid_amount] => 100
[performance_guaranteed] => 25
[isSmartBid] => 0
[user_id] => 18
[comment] => I can do this work
[isWinner] =>
[created] => 2012-02-08 14:16:39
)
[2] => Array
(
[id] => 5
[comment_group_id] => 2
[bid_status_id] => 1
[project_id] => 1
[bid_amount] => 45
[performance_guaranteed] => 10
[isSmartBid] => 1
[user_id] => 18
[comment] => This is a smart bid bid
[isWinner] => 0
[created] => 2012-02-25 15:40:54
)
)
[PaymentTransaction] => Array
(
[0] => Array
(
[id] => 1
[user_id] => 19
[account_type_id] =>
[entity_id] => 1
[payment_type_id] => 1
[entity_type_id] => 1
[payment_to_id] =>
[payment_status_id] => 2
[payment_number] =>
[service_id] => 4
[created] => 2012-02-10 18:45:52
[description] => Payment for project
[credit] => 100
[debit] => 0
[billing_address] =>
[billing_city] =>
[billing_state] =>
[billing_country_id] =>
[billing_name] =>
[exp_year] =>
[exp_month] =>
[security_code] =>
)
)
[WorkAcceptanceLog] => Array
(
)
[Comment] => Array
(
[0] => Array
(
[id] => 1
[comment_group_id] => 1
[project_id] => 1
[to_id] => 19
[from_id] => 20
[cc_id] =>
[isArbitration] =>
[no_contest] =>
[subject] => Re:
[comment] => Hi, I was writing about your project
[created] => 2012-02-08 14:15:04
[file_name] =>
[parent_id] =>
[last_message] =>
)
[1] => Array
(
[id] => 2
[comment_group_id] => 2
[project_id] => 1
[to_id] => 19
[from_id] => 18
[cc_id] =>
[isArbitration] =>
[no_contest] =>
[subject] => Re:
[comment] => I can do this work
[created] => 2012-02-08 14:16:39
[file_name] =>
[parent_id] =>
[last_message] =>
)
[2] => Array
(
[id] => 3
[comment_group_id] => 2
[project_id] => 1
[to_id] => 18
[from_id] => 19
[cc_id] =>
[isArbitration] =>
[no_contest] =>
[subject] => Broadcasted Message
[comment] => This is a broadcast to all consultants in regards to the project.
[created] => 2012-02-08 14:18:52
[file_name] =>
[parent_id] =>
[last_message] => 2012-02-08 14:18:51
)
[3] => Array
(
[id] => 4
[comment_group_id] => 1
[project_id] => 1
[to_id] => 20
[from_id] => 19
[cc_id] =>
[isArbitration] =>
[no_contest] =>
[subject] => Broadcasted Message
[comment] => This is a broadcast to all consultants in regards to the project.
[created] => 2012-02-08 14:18:52
[file_name] =>
[parent_id] =>
[last_message] => 2012-02-08 14:18:52
)
[4] => Array
(
[id] => 7
[comment_group_id] => 0
[project_id] => 1
[to_id] => 19
[from_id] => 27
[cc_id] => 20
[isArbitration] => 1
[no_contest] =>
[subject] => Arbitration
[comment] => What seems to be the issue ?
[created] => 2012-02-12 18:28:11
[file_name] =>
[parent_id] =>
[last_message] =>
)
[5] => Array
(
[id] => 8
[comment_group_id] => 5
[project_id] => 1
[to_id] => 17
[from_id] => 19
[cc_id] => 20
[isArbitration] => 1
[no_contest] =>
[subject] => Arbitration
[comment] => Well I would like to say I am not happy
[created] => 2012-02-12 19:35:51
[file_name] =>
[parent_id] =>
[last_message] =>
)
[6] => Array
(
[id] => 9
[comment_group_id] => 6
[project_id] => 1
[to_id] => 17
[from_id] => 19
[cc_id] => 20
[isArbitration] => 1
[no_contest] =>
[subject] => Arbitration
[comment] => I would like to make a complaint
[created] => 2012-02-12 22:07:08
[file_name] =>
[parent_id] =>
[last_message] =>
)
[7] => Array
(
[id] => 10
[comment_group_id] => 2
[project_id] => 1
[to_id] => 19
[from_id] => 18
[cc_id] =>
[isArbitration] =>
[no_contest] =>
[subject] => Re:Broadcasted Message
[comment] => This is a smart bid bid
[created] => 2012-02-25 15:40:54
[file_name] =>
[parent_id] =>
[last_message] =>
)
)
)
as you can see it ignores my attachment. what am i doing wrong.
This is a fairly old question, but for what its worth.
Here is the documentation for the Containable Behavior.
It states that, to load the behavior on the fly, you should use:
$this->Model->Behaviors->load('Containable');
I believe the difference is merely due to the fact that attach and detach have been deprecated.
Also, why not just set the $actAs property in your model, or even in AppModel?
class AppModel extends Model {
public $actAs = array('Containable');
// public $recursive = 0;
//...
}
The relation with the User model defined in the Project model is missing the foreignKey field, which may cause trouble. I assume Cake defaults that to user_id which should of course be the name of the column in the projects table.
Other than that, your use of the contain options seems ok.
Try this:
$this->Project->Behaviors->attach('Containable');
$this->Project->contain(array(
'conditions' => array('Project.id' => $projid),
'User'));
$project = $this->Project->find('first');
$this->Project->Behaviors->detach('Containable');

How to get HABTM associated data using hasOne binding

I am following example documented at http://book.cakephp.org/view/83/hasAndBelongsToMany-HABTM
I am trying to retrieve associated data using hasOne.
I created 3 tables posts, tags and posts_tags.
I wrote following code to debug Posts.
$this->Post->bindModel(array(
'hasOne' => array(
'PostsTag',
'FilterTag' => array(
'className' => 'Tag',
'foreignKey' => false,
'conditions' => array('FilterTag.id = PostsTag.tag_id')
))));
$output=$this->Post->find('all', array(
'fields' => array('Post.*')
));
debug($output);
I was expecting output something like below.
Array
(
0 => Array
{
[Post] => Array
(
[id] => 1
[title] => test post 1
)
[Tag] => Array
(
[0] => Array
(
[id] => 1
[name] => php
)
[1] => Array
(
[id] => 2
[name] => javascript
)
[2] => Array
(
[id] => 3
[name] => xml
)
)
}
But my output do not have Tags at all.
Here is what I got.
Array
(
[0] => Array
(
[Post] => Array
(
[id] => 1
[title] => test post1
)
)
[1] => Array
(
[Post] => Array
(
[id] => 2
[title] => test post2
)
)
)
How do I get associated tags along with the post.
I know I am missing something, but unable to figure out.
Any help would be highly appreciated.
Edit 1:
Ok I tried few more variants.
I tried:
$this->Post->bindModel(array(
'hasOne' => array(
'PostsTag',
'FilterTag' => array(
'className' => 'Tag',
'foreignKey' => false,
'conditions' => array('FilterTag.id = PostsTag.tag_id')
))));
$output=$this->Post->find('all');
I got:
Array
(
[0] => Array
(
[Post] => Array
(
[id] => 1
[title] => test post1
)
[PostsTag] => Array
(
[id] => 1
[post_id] => 1
[tag_id] => 1
)
[FilterTag] => Array
(
[id] => 1
[name] => php
)
)
[1] => Array
(
[Post] => Array
(
[id] => 1
[title] => test post1
)
[PostsTag] => Array
(
[id] => 2
[post_id] => 1
[tag_id] => 2
)
[FilterTag] => Array
(
[id] => 2
[name] => javascript
)
)
[2] => Array
(
[Post] => Array
(
[id] => 1
[title] => test post1
)
[PostsTag] => Array
(
[id] => 3
[post_id] => 1
[tag_id] => 3
)
[FilterTag] => Array
(
[id] => 3
[name] => xml
)
)
)
I tried:
$output=$this->Post->find('all', array(
'fields' => array('Post.*', 'FilterTag.*'),
'recursive' => 1
));
I got:
Array
(
[0] => Array
(
[Post] => Array
(
[id] => 1
[title] => test post1
)
[FilterTag] => Array
(
[id] => 1
[name] => php
)
)
[1] => Array
(
[Post] => Array
(
[id] => 1
[title] => test post1
)
[FilterTag] => Array
(
[id] => 2
[name] => javascript
)
)
[2] => Array
(
[Post] => Array
(
[id] => 1
[title] => test post1
)
[FilterTag] => Array
(
[id] => 3
[name] => xml
)
)
)
Just in case I am missing something, here is my Posts controller:
class PostsController extends AppController {
var $name = 'Posts';
var $helpers = array('Html','Ajax','Javascript');
var $components = array( 'RequestHandler' );
function index() {
$this->Post->bindModel(array(
'hasOne' => array(
'PostsTag',
'FilterTag' => array(
'className' => 'Tag',
'foreignKey' => false,
'conditions' => array('FilterTag.id = PostsTag.tag_id')
))));
$output=$this->Post->find('all', array(
'fields' => array('Post.*', 'FilterTag.*'),
'recursive' => 1
));
}
}
And here is my Posts model:
class Post extends AppModel {
var $name = 'Post';
}
I still wonder why is the example from cookbook not working.
I would recommend checking 3 things:
See if you have set your
associations right (hasMany,
hasAndBelongsToMany)
Make sure you have set the Post HABTM Tag and Tag HABTM Post relationships correctly.
Check the value
of $recursive
Most of the examples in the CakePHP book require that you have your $recursive property set to 1.
Remove the fields
restriction: 'fields' =>
array('Post.*')
It's possible that Cake is restricting only the Post fields, which is why no Tag fields are showing up. Remove this restriction and try it again.
Let us know if this works for you.

Categories