last generate id manual query in cakephp - cakephp

I have apply manual query in cakephp and not get last generate id
My query is this
$areas = $this->Area->query("insert into areas (Area_ID,Parent_Area_ID,Area) values ('','".$Parent_Area_ID."','".$Area_name."')");
How i get last id ?

Try this code:
$this->Area->getInsertID();
More detail here:
http://book.cakephp.org/2.0/en/models/additional-methods-and-properties.html#model-getinsertid

Try this
echo $this->Area->getLastInsertID();
If this doesn't work check out this answer from a similar question

Related

Access a field from another table using the object relation

I am new to SalesForce and SOQL so sorry in advance if the question has already been answered, if yes link it to me.
The aim of my SOQL query is to get all the contract information to generate PDF.
There are tables: Contract, Contact and Account
In the Contract table there are fields: Maitre_d_apprentissage__c, MaitreApprentissageNom1__c, MaitreApprentissagePrenom1__c, Apprenti__c, ApprentiNom__c, ApprentiPrenom__c
There are relationships:
Apprenti__r which link Apprenti__c to Contact table
Maitre_d_apprentissage__r which link Maitre_d_apprentissage__c to Contact table
When I looked at table, I saw that MaitreApprentissageNom1__c was equal to Maitre_d_apprentissage__r.LastName and ApprentiNom__c was equal to Apprenti__r.LastName. So I conclude I could get other information of Apprenti__c and Maitre_d_apprentissage__c from the Contact Table following the same principle. So I added to my query Apprenti__r.Date_de_naissance__c and Maitre_d_apprentissage__r.Date_de_naissance__c to get the Date_de_naissance__c field which is in my Contact table.
I see in the results that the query succeeds in getting the information but some values have changed column (lines 6 and 7), you can see the difference between query 1 and query 2. In the first query I only return the Apprenti__r.Date_de_naissance__c and in the second query I return Apprenti__r.Date_de_naissance__c and Maitre_d_apprentissage__r.Date_de_naissance__c
Query 1:
SELECT ApprentiNom__c, ApprentiPrenom__c, Apprenti__r.Date_de_naissance__c, MaitreApprentissageNom1__c, MaitreApprentissagePrenom1__c
FROM Contract
Result 1:
Query 2:
SELECT ApprentiNom__c, ApprentiPrenom__c, Apprenti__r.Date_de_naissance__c, MaitreApprentissageNom1__c, MaitreApprentissagePrenom1__c, Maitre_d_apprentissage__r.Date_de_naissance__c
FROM Contract
Result 2:
I would like to understand from where is coming the problem and how to correct it. Thank you in advance.
It's possible that it's just your query editor displaying stuff incorrectly. You can see it got confused with 2 lookups to Contact table, why there's even a column header "Contact.Date_de_naissance__c" (and why it's there twice). And they aren't shown in the order you requested...
What editor you're using? You could try built-in "Developer Console" or http://workbench.developerforce.com/
What do you need it for? In Apex order of fields won't matter, in REST API query the values fetched via lookup will come as JSON sub-objects so there will always be a way to figure out exactly which value is coming from which relation.
In Dev Console try to run this and check if it solves your fears:
System.debug(JSON.serializePretty([SELECT
ApprentiNom__c, ApprentiPrenom__c,
Apprenti__r.Date_de_naissance__c,
MaitreApprentissageNom1__c, MaitreApprentissagePrenom1__c,
Maitre_d_apprentissage__r.Date_de_naissance__c
FROM Contract]));
Then add Maitre_d_apprentissage__r.LastName to query and see what changed, what stayed as is.

How to Get Last Update id in Cake PHP

I want to update set of Records, so iam using following code and then i need update the updated row id to another table, so i want to Last Updated Row ID
$this->mymodel->updateAll(array('field1'=>0),array('field5'=>0));
In order to get the ID of the last row, you can simply use this getID function:
$this->(Model Name)->getID()
Check out the following reference for more relevant functions: http://book.cakephp.org/2.0/en/models/additional-methods-and-properties.html
Peace! xD :)

Divide by from selected columns

Sorry if the question was asked before i try search but not found. I'm still new on mssql so
maybe the answer are obvious just i cant find.
My current query is:
SELECT gold_min, gold_max, gold_min_2, gold_max_2
FROM m_rdb
I want to divide the values on current columns by 20 and then update, just not sure how to do.
Any help are welcome.
Are you looking for this:
update m_rdb
set col=col/20.0
If your updating the values to same columns then
Update m_rdb SET gold_min = gold_min/20, gold_max=gold_max/20,
gold_min_2=gold_min_2/20, gold_max_2=gold_max_2/20
If your updating the values to Other columns then
Update m_rdb SET gold_min_other = gold_min/20, gold_max_other=gold_max/20,
gold_min_2_other=gold_min_2/20, gold_max_2_other=gold_max_2/20

In CakePHP, is there a better way to select the users name?

I currently have a users table and a posts table. My posts table has a field for user_id, however, I am not sure how I would grab the users name field which is in the users table. I was thinking of using the models afterFind() method and then using SQL to select the data, but there has to be a better way than this. Also, on my view action, I am using the read() function to grab a single post. Would the models afterFind() kick in after it runs read()? If not, is there an equivalent such as afterRead()?
Just make an association of Post belongsTo User, and every regular find/read operation that has a sufficiently high recursive value will automatically fetch the user with each post.
$post = $this->Post->find(...);
echo $post['User']['name'];
you have to go like below :
$this->Cake->findById(7); Cake.id = 7
here , you can you use your user_id instead of 7 like..
Find BY CAKE PHP
$this->Post->bindModel(array('belongsTo'=>'User'));
$post = $this->Post->findById($post_id);
$userName = $post['User']['name'];
Here are errors, I've typed it in eclipse (:

get ids of all inserted records by last query executed in cakephp

I can find the primary key id of last inserted record as follows:
$this->Model->save($record);
$id = $this->Model->getLastInsertId();
I am looking for something like this:
$this->Model->saveAll($records);
$ids = $this->Model->getLastInsertIds();
I am inserting 100s of records, so its better to insert them all in a single query. Still its taking some seconds to execute the use case. Is there a way to get all ids of inserted records by last query?
This was asked in many forums, but there are no clear answer.
I think this might work and might be what you are looking for:
$this->Model->saveAll($records);
$id1 = $this->Model1->id();
$id2 = $this->Model2->id();
$id3 = $this->Model3->id();
...
Remember to replace model1,model2,model3 with each of the models that you are saving to.
Have you looked at the cakephp tutorial on saving your data? here is the link:
http://book.cakephp.org/view/1031/Saving-Your-Data
Hope this helps.

Resources