i am newbie to cakephp. I need to use assoc for 3 tables I want result to be in following way
`[0] => Array
(
[User] => Array
(
[id] => 45
[email] => nonita#gmail.com
)
[Product] => Array
(
[id] => 5
[user_id] => 45
[description] => wegjhj
)
[Comment] => Array
(
[0] => Array
(
[id] => 1
[comment] => 'kjfkjfkfd'
[product_id] => 5
)
[1] => Array
(
[id] => 2
[comment] => 'kjfkjfkfd'
[product_id] => 5
)
)
)
)`
how can I create assoc in cakephp and is it possible to create array like this using assoc?
i want to fetch user name , product name and the comment that user has given on the product
In the Product model bind the model to User Model as belongsTo association
public $belongsTo = array(
"User" => array(
"className" => "User",
"foreignKey" => "user_id"
)
);
and to Comment Model as hasMany association
$public $hasMany = array(
"Comment" => array(
"className" => "Comment",
"foreignKey" => "product_id"
)
);
and run the find query on Product Model as
$this->Product->find('all');
Related
My code is:
$costSheetQuery = $this->CostSheets->find('all')->where(['id IN' => $criteria]);;
$costSheetData = $costSheetQuery->all();
I print the array $costSheetData and it gives me the following relust:
Cake\ORM\ResultSet Object
(
[items] => Array
(
[0] => App\Model\Entity\CostSheet Object
(
[id] => 5
[key_value] => {"sample_date":"10-03-2018","mh_sc":"mh-sc","buyer":"buyer","season":"season","style":"styel","style_detail":"Style Details","sample_img":[[""]],"fab_category":[["FABRIC CATEGORY","FABRIC CATEGORY","FABRIC CATEGORY","FABRIC CATEGORY","FABRIC CATEGORY","FABRIC CATEGORY"]],"patt":[["PATT","PATT","PATT","PATT","PATT","PATT"]],"size":[["SIZE","SIZE","SIZE","SIZE","SIZE","SIZE"]],"ship_date":[[{"year":"2018"},{"month":"01"},{"day":"01"}]],"piece":[["10","20","30","40","50","60"]],"fabric_usage":[["USAGE"]],"fabric_yds":[["yds"]],"fabric_body":[["Body","Body","Body","Body","Body","Body"]],"cm":[["embrotery"]],"cm_value":[[" 10 ","12.00","","15","",""]],"trims":[["thread"]],"trims_value":[["10","","10","20","",""]]}
[created] => Cake\I18n\FrozenTime Object
(
[time] => 2018-03-10T16:47:52+00:00
[timezone] => UTC
[fixedNowTime] =>
)
[modified] => Cake\I18n\FrozenTime Object
(
[time] => 2018-03-10T16:47:52+00:00
[timezone] => UTC
[fixedNowTime] =>
)
[[new]] =>
[[accessible]] => Array
(
[key_value] => 1
[created] => 1
[modified] => 1
)
[[dirty]] => Array
(
)
[[original]] => Array
(
)
[[virtual]] => Array
(
)
[[errors]] => Array
(
)
[[invalid]] => Array
(
)
[[repository]] => CostSheets
)
)
)
Whats hell is going on. I just start hating cakephp. My additional bullshit in my array? I also try to separate as : $items->costSheetData as item is an object of $costSheetData. But it gives me an arrow. Any idea?
This is my function for login i call a stored procedure login for match user name and password, but it doesn't work for me it give me all the rows:
public function login($email,$password)
{
$consumers = TableRegistry::get('Consumers');
$result=$consumers->query("Call login('".$email."','".$password."')");
pr($result->toArray());die;
}
My stored procedure in phpmyadmin is below:
BEGIN
SELECT * FROM consumers WHERE email = email_id AND password = md_password;
END
When i am executing query it give me object but after converting this object into array it give me all the row of table.output is:
<pre class="pr">Array
(
[0] => Cake\ORM\Entity Object
(
[_properties:protected] => Array
(
[id] => 1
[name] => jeevan
[email] => j#gmail.com
[password] => asdf
[phone_no] => 8447726137
[ota] => cde
[status] => 0
[created_on] => Cake\I18n\FrozenTime Object
(
[date] => 2016-07-08 17:28:52
[timezone_type] => 3
[timezone] => UTC
)
[token_access] =>
[device_type] => 1
[push_id] => abc
[want_news] => 1
[postal_code] => 263136
[registration_type] => 1
)
[_original:protected] => Array
(
)
[_hidden:protected] => Array
(
)
[_virtual:protected] => Array
(
)
[_className:protected] =>
[_dirty:protected] => Array
(
)
[_new:protected] =>
[_errors:protected] => Array
(
)
[_invalid:protected] => Array
(
)
[_accessible:protected] => Array
(
[*] => 1
)
[_registryAlias:protected] => Consumers
)
[1] => Cake\ORM\Entity Object
(
[_properties:protected] => Array
(
[id] => 2
[name] => jack
[email] => jack#gmail.com
[password] => 123
[phone_no] => 7409757656
[ota] => chb
[status] => 1
[created_on] => Cake\I18n\FrozenTime Object
(
[date] => 2016-07-20 06:10:14
[timezone_type] => 3
[timezone] => UTC
)
[token_access] => ghcvhgv
[device_type] => 0
[push_id] => hgnjh
[want_news] => 1
[postal_code] => 263136
[registration_type] => 1
)
[_original:protected] => Array
(
)
[_hidden:protected] => Array
(
)
[_virtual:protected] => Array
(
)
[_className:protected] =>
[_dirty:protected] => Array
(
)
[_new:protected] =>
[_errors:protected] => Array
(
)
[_invalid:protected] => Array
(
)
[_accessible:protected] => Array
(
[*] => 1
)
[_registryAlias:protected] => Consumers
)
)</pre>
that mean the stored procedure not working, any idea really help me, thanks in advance!
You can call a stored procedure using the ConnectionManager's execute() method. It's important to remember to prepare the query so that you remove the risk of SQL injection:-
$this->connection = ConnectionManager::get('default');
$results = $this->connection->execute(
'CALL login(?, ?)',
[$email, md5($password)]
)->fetchAll('assoc');
When CakePHP runs execute() it will substitute the ? with the escaped and quoted values of $email and md5($password). For further details check out the official docs on Preparing a statement.
step 1:use the class as use Cake\Datasource\ConnectionManager;
step 2: then execute the procedure-
$this->connection = ConnectionManager::get('default');
$consumer = $this->connection->execute("CALL login('".$email."','".md5($password)."')")->fetchAll('assoc');
now it will give an array.
finally i got this answer in cakephp cookbook.
Think you can give me your help on MongoDB .
My scheme is for example :
Array
(
[_id] => MongoId Object
(
[$id] => 538339ef5b17848a488b7e35
)
[group_name] => 'group 1'
[activity] => Array
(
[open] => Array
(
[company] => Array
(
[0] => Array
(
[name] => company 1
[date] => MongoDate Object
(
[sec] => 1400081160
[usec] => 0
)
),
[0] => Array
(
[name] => company 2
[date] => MongoDate Object
(
[sec] => 1500081160
[usec] => 0
)
),
[0] => Array
(
[name] => company 3
[date] => MongoDate Object
(
[sec] => 1600081160
[usec] => 0
)
),
[0] => Array
(
[name] => company 4
[date] => MongoDate Object
(
[sec] => 1700081160
[usec] => 0
)
)
)
)
)
)
Array
(
[_id] => MongoId Object
(
[$id] => 538339ef5b178424498b7e34
)
[group_name] => 'group 2'
[activity] => Array
(
[open] => Array
(
[company] => Array
(
[0] => Array
(
[name] => company 3
[date] => MongoDate Object
(
[sec] => 1300081160
[usec] => 0
)
),
[0] => Array
(
[name] => company 1
[date] => MongoDate Object
(
[sec] => 1600081160
[usec] => 0
)
),
[0] => Array
(
[name] => company 2
[date] => MongoDate Object
(
[sec] => 1800081160
[usec] => 0
)
),
[0] => Array
(
[name] => company 4
[date] => MongoDate Object
(
[sec] => 1900081160
[usec] => 0
)
)
)
)
)
)
And infact I would fill display the "groupname" only if such insertion order by DATE is "Company 1, Company 2, Company 3".
So normally it should show me in the example above only "Group 1", because the order is as required, while in the "Group 2" order starts with "Business 3, followed by Company 1. ... ".
--
For example if I'm looking like this:
[activity.open.company] => Array
(
[$in] => Array
(
[0] => company 1
[1] => company 2
[2] => company 3
)
)
This will highlight my whole world and not just the "Group 1"
--
Thank you sincerely for your help!
PS : Sorry for my english, I'm french.
When you query with an $in clause it basically becomes and $or on the items of the given array. The query you have written would match the two documents listed because they both have at least company 2 in them.
I executed following statement
$posts = Post::model()->with(array( 'category', 'member', 'profile' ))->findAll();
I printed $posts in log file. I got following result.
Array
(
[0] => Post Object
(
[_new:CActiveRecord:private] =>
[_attributes:CActiveRecord:private] => Array
(
[id] => 1
[post_text] => Sales needs to be increase by the end of this month. I don't wnat any reason behind this.
[member_id] => 2
[category_id] => 3
[published] => 0
[draft] => 1
[date_added] => 2014-04-06
)
[_related:CActiveRecord:private] => Array
(
[category] => Category Object
(
[_new:CActiveRecord:private] =>
[_attributes:CActiveRecord:private] => Array
(
[id] => 1
[category_name] => Anything
[type] => General
[date_added] => 2014-04-05
)
[_related:CActiveRecord:private] => Array
(
)
[_c:CActiveRecord:private] =>
[_pk:CActiveRecord:private] => 1
[_alias:CActiveRecord:private] => t
[_errors:CModel:private] => Array
(
)
[_validators:CModel:private] =>
[_scenario:CModel:private] => update
[_e:CComponent:private] =>
[_m:CComponent:private] =>
)
[member] => Member Object
(
[_new:CActiveRecord:private] =>
[_attributes:CActiveRecord:private] => Array
(
[id] => 1
[screen_name] => prashantb
[email] => prashantbharambe22#gmail.com
[date_added] => 2014-04-05
[first_name] => prashant
[last_name] => bharambe
)
[_related:CActiveRecord:private] => Array
(
[profile] => MemberProfile Object
(
[_new:CActiveRecord:private] =>
[_attributes:CActiveRecord:private] => Array
(
[id] => 1
[member_id] => 1
[city] => kalyan
[state] => maharashtra
[country] => india
[designation] => php developer
[date_added] => 2014-04-05
)
[_related:CActiveRecord:private] => Array
(
)
[_c:CActiveRecord:private] =>
[_pk:CActiveRecord:private] => 1
[_alias:CActiveRecord:private] => t
[_errors:CModel:private] => Array
(
)
[_validators:CModel:private] =>
[_scenario:CModel:private] => update
[_e:CComponent:private] =>
[_m:CComponent:private] =>
)
)
[_c:CActiveRecord:private] =>
[_pk:CActiveRecord:private] => 1
[_alias:CActiveRecord:private] => t
[_errors:CModel:private] => Array
(
)
[_validators:CModel:private] =>
[_scenario:CModel:private] => update
[_e:CComponent:private] =>
[_m:CComponent:private] =>
)
)
[_c:CActiveRecord:private] =>
[_pk:CActiveRecord:private] => 1
[_alias:CActiveRecord:private] => t
[_errors:CModel:private] => Array
(
)
[_validators:CModel:private] =>
[_scenario:CModel:private] => update
[_e:CComponent:private] =>
[_m:CComponent:private] =>
)
)
But I need a result in following format.
Array
(
[0] => Array
(
[id] => 1,
[post_text] => Sales needs to be increase by the end of this month. I don't wnat any reason behind this,
[member_id] => 2,
[category_id] => 3,
[published] => 0,
[draft] => 1,
[date_added] => 2014-04-06,
[category] => Array (
[id] => 1,
[category_name] => Anything,
[type] => General,
[date_added] => 2014-04-05,
),
[member] => Array (
[id] => 1,
[screen_name] => prashantb,
[email] => prashantbharambe94#gmail.com,
[date_added] => 2014-04-05,
[first_name] => prashant,
[last_name] => bharambe,
[profile] => Array(
[id] => 1
[member_id] => 1
[city] => kalyan
[state] => maharashtra
[country] => india
[designation] => php developer
[date_added] => 2014-04-05
)
)
)
)
In short I need all attributes values in one array. ( Model Attributes and its related model attributes )
I am new to yii. Please help me on this.
Yii doesn't work with arrays. Each DB record is represented by an ActiveRecord object. This way you can access your column values like this:
echo $model->post_text; // Sales needs to be increase...
echo $model->date_added; // 2014-04-06
echo $model->etc...
If you did set up relations right, you can access that data like this:
echo $model->category->category_name; // Anything
echo $model->member->screen_name; // prashantb
You have to create the array you showed manually. If you still need the data in an array, you can also access the attributes value:
print_r($model->attributes); // array( [id] => 1, [post_text] => 'Sales needs to be increase', ...)
I'm trying to get to grips with the Kohana ORM but I'm a bit stuck.
I have created category and post models, with a relationship based on category_id.
I can fetch post and category models separately, however I cannot seem to fetch them using the relationship. For example:
// Get first category
$category = ORM::factory('category', 1);
foreach ($category->posts as $post)
{
echo $post->title;
}
The above just gives me a blank screen (I know I shouldn't be outputting anything in the controller, just trying to get it working first).
print_r($category);
Gives me this:
Model_Category Object ( [_has_many:protected] => Array ( [posts] => Array ( [model] => post [foreign_key] => category_id [through] => [far_key] => post_id ) ) [_has_one:protected] => Array ( ) [_belongs_to:protected] => Array ( ) [_load_with:protected] => Array ( ) [_validate:protected] => [_rules:protected] => Array ( ) [_callbacks:protected] => Array ( ) [_filters:protected] => Array ( ) [_labels:protected] => Array ( ) [_object:protected] => Array ( [id] => 1 [title] => [description] => ) [_changed:protected] => Array ( ) [_related:protected] => Array ( ) [_loaded:protected] => [_saved:protected] => 1 [_sorting:protected] => Array ( [id] => ASC ) [_foreign_key_suffix:protected] => _id [_object_name:protected] => category [_object_plural:protected] => categories [_table_name:protected] => categories [_table_columns:protected] => Array ( [id] => Array ( [type] => int [min] => 0 [max] => 65535 [column_name] => id [column_default] => [data_type] => smallint unsigned [is_nullable] => [ordinal_position] => 1 [display] => 6 [comment] => [extra] => auto_increment [key] => PRI [privileges] => select,insert,update,references ) [title] => Array ( [type] => string [character_maximum_length] => 65535 [column_name] => title [column_default] => [data_type] => text [is_nullable] => [ordinal_position] => 2 [collation_name] => latin1_swedish_ci [comment] => [extra] => [key] => [privileges] => select,insert,update,references ) [description] => Array ( [type] => string [character_maximum_length] => 65535 [column_name] => description [column_default] => [data_type] => text [is_nullable] => [ordinal_position] => 3 [collation_name] => latin1_swedish_ci [comment] => [extra] => [key] => [privileges] => select,insert,update,references ) ) [_ignored_columns:protected] => Array ( ) [_updated_column:protected] => [_created_column:protected] => [_primary_key:protected] => id [_primary_val:protected] => name [_table_names_plural:protected] => 1 [_reload_on_wakeup:protected] => 1 [_db:protected] => Database_MySQL Object ( [_connection_id:protected] => d6ad44aa068ae69071b9614f2a4a760bf55d9307 [_identifier:protected] => [last_query] => SHOW FULL COLUMNS FROMcategories` [_instance:protected] => default [_connection:protected] => Resource id #36 [_config:protected] => Array ( [type] => mysql [connection] => Array ( [hostname] => localhost [database] => cms_test_kohana [persistent] => ) [table_prefix] => [charset] => utf8 [caching] => [profiling] => 1 ) ) [_db_applied:protected] => Array ( ) [_db_pending:protected] => Array ( ) [_db_reset:protected] => 1 [_db_builder:protected] => [_with_applied:protected] => Array ( ) [_preload_data:protected] => Array ( ) )
Can anyone help?
Any advice appreciated.
Thanks.
Posts is a relationship, not a collection. Use find_all() or find() on a relationship to return a collection of records.
foreach ($category->posts->find_all() as $post) {
echo $post->title;
}
You can also apply various other methods to the relationship to filter the returned results using orderby, where, etc...