Accessing attribute in $this->request - cakephp

I am a bloody CakePHP beginner starting with the latest CakePHP version 4.1.0.
I get the attribute for identity successfully with $identity = $this->request->getAttribute('identity');
Below the print_() dump for the identity object. And from here on I am stuck as I have no clue how to get the values id, name, email, ... inside the data object.
My intention is to get the current user properties. What did I understand wrong, how is the correct way to get the user data?
Authentication\Identity Object
(
[config] => Array
( fieldMap] => Array ( [id] => id )
)
[data] => App\Model\Entity\User Object
(
[id] => 1
[name] => Some Name
[email] => name#example.com
[password] => $2y$10$s******
[role] => admin
[created] => Cake\I18n\FrozenTime Object
(
[date] => 2020-07-27 15:23:59.000000
[timezone_type] => 3
[timezone] => UTC

Oh oh, it's much simpler than thought.
$identity->id|name|email is the solution.

Related

Cakephp-2.3 Form Pre filling doesnt load associated data

I have two models in HABT relation.Doctor and Patience.
I'm trying to create an edit form for Patient inside the Doctors controller.When I'm in Doctor controller I want to get all data of the Patients (not related Patients of Doctor )and its related Doctor. I tried the code below but it returns only Patient not its related data.
$options = array('recursive' => 2,'conditions' => array('Patience.' .$this->Doctor->Patience->primaryKey => $id));
$this->request->data = $this->Doctor->Patience->find('first', $options);
This only getting Patience model data not its related data.
Array ( [Patience] => Array ( [username] => akhil [password] => 93477851451f6583c7efd383e715cb6f8accb804 [id] => 4 [name] => akhil [age] => 33 [address] => w4ewrf [cellno] => 324323 [email] => awad#gmail.com [medicines] => sd [diseases] => sdf [misc] => sdf ) [Doctor] => Array ( ) )

Saving new associated records in CakePHP

So I have a Message model. When a message is created, I want to create new Recipient records. (Message hasMany Recipient, Recipient belongsTo Message).
The intent is that the message_id field in these associated records gets populated automagically with the new Message id.
I've tried to format the data every which way, and am aware of saveAll and saveAssociated methods, but nothing seems to work... Here's what I assumed would work:
Data passed on to save:
[Message] => array
(
[subject] => Foo bar
[body] => Blah blah blah.
)
[Recipient] => array
(
[0] => array
(
[user_id] => 1
)
[1] => array
(
[user_id] => 5
)
[2] => array
(
[user_id] => 6
)
)
Using saveMany, saveAll, saveAssociated didn't seem to make a difference (even with 'deep' set to true).
A few things:
1) The Message being created was a new record. I realized after that I didn't have $this->Message->create() in my controller. Now, I've since tried without and it works fine but I believe it's best practice to include it.
2) Since I need to save multiple associated records, I have to use saveAll or saveMany but doing so was causing extra blank rows to be added, since the main record was not numerically indexed (which is what saveAll wants).
So here's some code from my controller:
if ($this->request->is('post')) {
$this->Message->create();
if ($this->Message->saveAll($data, array('deep' => true))) {
// code here
}
}
And here is how my data is structured:
Array
(
[0] => Array
(
[Message] => Array
(
[subject] => Foo
[body] => bar
[Recipient] => Array
(
[0] => Array
(
[user_id] => 1
)
[1] => Array
(
[user_id] => 6
)
[2] => Array
(
[user_id] => 38
)
)
)
)
)
This will create a new Message and properly associate the newly created Recipient records.

CakePHP saving model using Session

I am saving form data in a Session, and trying to save to a model called Property using the session array. Please see the array below. I think it has something to do with the Session array but I am not sure.
When I try to save like so, it does not save:
$this->Property->save($propertyData) where $propertyData is the property array.
sql_dump:
INSERT INTO `fra`.`properties` (`type`, `address`, `city`, `state`, `zip`, `price`, `bed_rooms`, `bath_rooms`, `lot_size_sq_ft`)
VALUES ('0', '2720 Acapulco way', 'modesto', 'ca', '95355', 310000, 4, 3, 6040)
The session array is:
Array
(
[house_details] => Array
(
[form] => Array
(
[section] => house_details
)
[Property] => Array
(
[type] => 0
[address] => 2720 Acapulco way
[city] => modesto
[state] => ca
[zip] => 95355
[price] => 310000
[prop_year_build] => 2007
[prop_year_remodel] =>
[bed_rooms] => 4
[bath_rooms] => 3
[garage_spaces] => 3
[lot_size_sq_ft] => 6040
[house_size_sq_ft] => 3720
[stories] => 2
[condition_rating] => 8
)
You should be able to do this quite simply. You probably have a problem with $propertyData being set as the wrong thing so it doesn't provide a valid value for the model save.
What do you get if you do debug($propertyData)?
Does this work?
$propertyData = $this->Session->read('house_details.Property');
$this->Property->save(propertyData);

hasOne subtype on CakePHP

I have 3 users tables on my DB:
Users
users_twitter
users_web
Users is the parent of both users_twitter and users_web.
And then another table for comments. Both kind of users can comment.
So, when I want to show the comments I need to get information of the author of each comment.
In order to do this I am using the variable $belongsTo on the /Model/Comment.php like this:
public $belongsTo = array(
'users' => array(
'foreignKey' => 'idUser'
)
);
In Controller/CommentController, when i do this:
public function index($idPost) {
$this->Comment->recursive = 0;
return $this->Comment->find('all');
}
I get this kind of array:
[0] => Array
(
[Comment] => Array
(
[id] => 1
[idPost] => 441
[idUser] => 387371023
[comment] => hello word
[created] => 2012-03-01 00:00:00
)
[User] => Array
(
[id] => 1
[username] => myname
)
)
And what I want is to get more info yet, the one of the subtype of Users, depending on the existence of the ID on the subtype tables.
In case it was an id of a user_web table, I would like to get this:
[0] => Array
(
[Comment] => Array
(
[id] => 1
[idPost] => 441
[idUser] => 387371023
[comment] => hello word
[created] => 2012-03-01 00:00:00
)
[User] => Array
(
[id] => 1
[username] => myname
[users_web] => Array
(
[id] => 1
[username] => myName
[password] => XXXXX
[verified] => 1
[created] => 1
)
)
Do you know if that's possible with CakePHP?
As far as I know, with $hasOne I only get one level more, I need two.
In your AppModel add
public $actsAs = array('Containable');
In your index:
public function index($idPost) {
return $this->Comment->find('all', array(
'contain' => array('User.UsersWeb')));
}
Your User model has to be associated with a UsersWeb model to make this work.
See http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html#containing-deeper-associations for a complete description.
Also why do you return the data in the controller index? Are you using requestAction for this? And why don't you use pagination? Do you really want to display hundreds of comments on one page?
See http://book.cakephp.org/2.0/en/core-libraries/components/pagination.html

Cakephp 1.3 Save Multiple Model rows, some with an id and some without an id

Is it possible to update existing data and save new data at the same time without having to loop through the array? I would like the array with the id to update and the array without an id to create a new entry. See the example below. Thank you for your help.
[Workload] => Array
(
[0] => Array
(
[phase_count] => 1
[id] => 17
[value] => {"phases":[{"rep_range":"20-30","rep_set_count":"1"}]}
[user_id] => 1
)
[4] => Array
(
[phase_count] => 1
[value] => {"phases":[{"rep_range":"20-30","rep_set_count":"1"}]}
[user_id] => 1
)
);
and then this
$this->Workload->saveAll($this->data['Workload']);
EDIT ======================================
Here is the code that actually saves this array
if($this->data){
array_walk($this->data['Workload'], function (&$value,$index){
// This will need to be changed once users are setup
if(empty($value['user_id'])){
$value['user_id'] = 1;
}
$value['value'] = json_encode($value['value']);
});
debug($this->data);
$this->Workload->saveAll($this->data['Workload']);
In fact, I do exactly that in one of my apps. If there is no 'id' a table entry will be created. If the 'id' is specified, the record will be updated.

Resources