How can I insert/update external table while do a node_save - drupal-7

I need to save two details about my node but I don't want to do so in the traditional way (I don't want to use the field_data tables). I would like to create a new table with the nid as foreign key and the two columns with the details.
So is there any way to insert into this custom table while we are calling node_save functionality? Also is it possible to call these details while we call node_load functionality?

you can use drupal hook for custom table update or insert new row like
function hook_insert($node) {
db_insert('mytable')
->fields(array(
'nid' => $node->nid,
'extra' => $node->extra,
))->execute();
$num_updated = db_update('node') // Table name no longer needs {}
->fields(array(
'uid' => 5,
'status' => 1,
))
->condition('nid', $node->nid)
->execute();
}

Related

How to order a CakePHP query by an aggregate function on an associated table's field?

I want to order by an aggregate function on an associated table's field, but when I debug the SQL query being executed, the associated table, PersonaHistory doesn't even get JOINed, and of course I don't get any results.
Is there a way to achieve this?
Can I force a table to be joined in the query?
$query = $this->Personas->find('all')
->contain(['PersonaHistory'])
->order(['MAX(PersonaHistory.updated)' => 'ASC'])
->group('PersonaHistory.persona_id');
DB: Personas has many PersonaHistory
It looks as if Personas is related to PersonaHistory via hasMany. Only hasOne and belongsTo associations produce a JOIN statement.
A workaround is to rewrite your query as:
$query = $this->PersonaHistory->find('all')
->contain(['Personas'])
->order(['MAX(PersonaHistory.updated)' => 'ASC'])
->group('PersonaHistory.persona_id');
your table name need to be plurialized and you can execute your order and group directly on your associated table field
<?php $query = $this->Personas->find('all')->contain([
'PersonaHistories' => function ($q) {
return $q->order(['Max(updated)' => 'ASC'])
->group('persona_id');
}]);

Laravel Eloquent - Using pivot table's id as a foreign key in another table

LARAVEL 5
I have a table that's belong to few other tables , at the moment of add new recored into it need all other table's id too , the tables structure are :
agreement-table:
user_id,
Client_id,
Owner_id,
property_id,
date ,
....( all other columns no related to this conversation )
User-table:
id,
...
Client-table:
id,
Role ( which can be client or Owner)
...
property-table:
id,
...
what is the right way to make relation with agreement-table? and how can I insert/update data into agreement-table?
what are the Model/Controller?
I tried to update it manually in this way but it just insert 0 into records : ( the data send by post or session all working right but not able to insert into database).
$agreement= new RentalAgreement(array(
'client_id' => Session::get('client_id'),
'owner_id' => Session::get('owner_id'),
'property_id' => $request->property,
'user_id' => Auth::user()->id,
.
.
.
this is the database migration :
Schema::create('rental_agreements', function(Blueprint $table)
{
$table->increments('id');
$table->integer('client_id');
$table->integer('owner_id');
$table->integer('property_id');
$table->integer('user_id');
.
.
.
for the manually update just need to add the columns name to the model's $fillable list this
$fillable=[ 'client_id','user_id',........ ];

Best rules to get data with Contain

In CakePHP 3 ORM has changed and I can't find the proper way to select needed data from the database.
In CakePHP 2, I use contain('User.name','User.id'), but In CakePHP 3 this code doesn't work.
So how can I select only id and name from User?
The code:
$query = $data->find()->contain(['Users'])->execute()->fetchAll('assoc');
// I want only user.id and user.name.
$articles = $this->Model->find()
->select(['fields_you_want_from_this_Model'])
->contain(['Assoc_Model' => function($q) {
return $q
->select(['fields_you_want_from_the_associated_model']);
}]);
U must take a look about this page: http://book.cakephp.org/3.0/en/orm/query-builder.html#passing-conditions-to-contain
In certain case you must use autoFields method.
Be carefull with contain when u select few fields in the callable, u always have to select the foreign key also:
When you limit the fields that are fetched from an association, you must ensure that the foreign key columns are selected. Failing to select foreign key fields will cause associated data to not be present in the final result.

Update record in cake php using key other than row id

I have a database table in which along with the data, provider_id is also unique. Now I have a set of data in which I got provider_id not the row id. So is it possible to edit the row using this provider_id ?
If you don't want to use the primary key, then seems that updateAll() is your friend
http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-updateall-array-fields-array-conditions
Get the row you want to edit by doing something like:
$row = $this->ModelName->findByProviderId($data['provider_id']);
Now modify $row however you like:
$row['some_field'] = 'someValue';
Then you can use Model::save() like:
$this->ModelName->save($row);
As long as $row will have the primaryKey field of your model, which is generally id, Model::save() will perform an update.
Try this:
Let the model be "Contact".
$this->Contact->updateAll(
array( 'Contact.any field' => 'value' ), //fields to update ex. Contact.name
array( 'Contact.provider_id' => $id ) //condition $id is the provider_id
);
Thanks

Cakephp Select foreign key and order by name!

I can get drop downs from foreign keys but how to sort them by name instead of id.
Adding 'order' => 'Country.name asc' in belongsTo model doesn't work, or I'm missing something!!!
e.g. Choose country, state, city, and those to be sorted by name, instead of id.
Are you scaffolding? If not should have a call similar to
$this->set('groups', $this->User->Group->find('list'));
in your controller simply add the sort condition to the find call e.g.
$this->set('groups', $this->User->Group->find('list',array('order'=>'Group.name desc')));

Resources