Mysql_insert_id with Doctrine - php

I have a couple of tables (mySQL) that i would like to update with the help of Doctrine. The products table id is auto-incrementing, and here's a brief description on what I would like to do:
$prod = new Products();
$prod->type = '0';
$categ = new CategoriesToProducts();
$categ->cat = '111';
$categ->product = $prod->id;
$conn = Doctrine_Manager::connection();
$conn->flush();
How can I do this while using flush? Using a regular save is an alternative, but there will be multiple transactions while doing such.
I have tried to find a Mysql_insert_id version for doctrine, but without any luck.
Thanks!

Here you can find some information: http://www.doctrine-project.org/documentation/manual/2_0/en/basic-mapping#identifiers-/-primary-keys

Related

Case insensitive approach for sugarcrm retrieve_by_string_fields() function

Is there any case-insensitive approach for the bean method retrieve_by_string_fields() in sugarcrm. What we want is that when retrieving using a name Akshay and akshay should both return the same bean.
Kindly guide me on this. Thanks.
Not sure it can be done using retrieve_by_string_fields, though one solution would be to use sql to first get the bean-id, and then fetch the bean using the id.
Example using accounts module;
$name = "Akshay";
$db = DBManagerFactory::getInstance();
$id = $db->getOne("SELECT id FROM accounts WHERE name = '$name' AND deleted = 0");
if (!empty($id)) {
$account = BeanFactory::getBean("Accounts", $id);
}

Phalcon PHP: Update a primary natural key

Is there a good way to be able to update a table which has a natural key in Phalcon?
Consider this table:
people
------
person
created_at
updated_at
We're going to assume that the person field is unique and is the primary key. I try to do the following:
$person = new People();
$person->person = 'Ed';
$person->save();
$personUpdate = People::findFirst('person = "Ed"');
$personUpdate->person = 'Bob';
$person->save();
What Phalcon ends up trying to do is to INSERT a new record, rather than to update the existing record. What I need it to do is to UPDATE ... WHERE person = 'Ed';
Thoughts?
Thanks!
Try the following...
<?php
$personUpdate = People::findFirst('person = "Ed"');
$personUpdate->person = 'Bob';
$person->update();
You are doing correct except ... People::find
find will prepare to fetch all data.. this means its in array Documentation
You need to use findFirst instead of find
$personUpdate = People::findFirst('person = "Ed"');
$personUpdate->person = 'Bob';
$person->save();
Please note that you are using $person->update() instea of $personUpdate->update();
<?php
$personUpdate = People::findFirst('person = "Ed"');
$personUpdate->person = 'Bob';
$person->update();

how to make sql query with and in yii active record?

I am new in Yii. Now i have encountered problem with active record in yii.
So , I have normal sql here :
$sqlText = "SELECT *
FROM tbl_webservicetokens
WHERE clienttoken = '{$appToken}'
AND
systimestamp < expiredate";
I want to use active record. But i endeavored
$post=TBLWEBSERVICETOKENS::model()->find(
'CLIENTTOKEN=:appToken AND EXPIREDATE>:systimestamp',
array(
':appToken'=>$appToken,
':systimestamp'=>'systimestamp'));
But i had error!
any idea?
Try this query. I think this will work for you.
$post= TBLWEBSERVICETOKENS::model()->find(array(
'select'=>'*',
'condition'=>'CLIENTTOKEN=:appToken AND EXPIREDATE>:systimestamp',
'params'=>array(':appToken'=>$appToken,':systimestamp'=>'systimestamp'))
);
You are passing wrong data type (string) for the date time field have you noted that
$post=TBLWEBSERVICETOKENS::model()->find(
'CLIENTTOKEN=:appToken AND EXPIREDATE>:systimestamp',
array(
':appToken'=>$appToken,
':systimestamp'=>$systimestamp)); //$systimestamp where your time data type value resides
If you still have the error
Read Me!!!
Well, It seems to me i find answer:
$criteria = new CDbCriteria;
$criteria->condition = "ID = 1212 AND CLIENTTOKEN = 'ws546b041c85ad38a2c1f4224e1e39fe09cf76a3c8703c5'";
$models = TBLWEBSERVICETOKENS::model()->findAll($criteria);

How to export data from user modx database to form?

I've table in modx database (orders), and i need to export data from that db to table at site.
I pushing into db with following snipept
<?php
function agregarCargas( &$fields )
{
global $modx;
// Init our array
$dbTable = array();
$dbTable['subject'] = $modx->db->escape($fields['subject']);
$dbTable['fullname'] = $modx->db->escape($fields['fullname']);
$dbTable['message'] = $modx->db->escape($fields['message']);
// Run the db insert query
$dbQuery = $modx->db->insert($dbTable, 'orders' );
return true;
}
?>
How can i export from DB? Snippet or? Thanks.
(Old thread, just for new folks trying to tackle this...)
Looking at the API you're using I'm guessing you are stuck with an old MODx version. (Evolution)
You should take a look at the API::DB docs for MODX Evolution
Something along the lines of the following would fill your HTML table:
$res = $modx->db->select("subject, fullname", 'orders');
$res_rows = $modx->db->makeArray($res);
$rows = "";
for($n=0;$n<count($res_rows);$n++){
$rows .= "<tr><td>".$res_rows['subject']."</td><td>".$res_rows['fullname']."</td></tr>\n";
}
return $rows;
(Of course you should use chunks instead of hardcoded HTML)

Fetching Collection of SugarCRM Beans

Programmatically speaking, is there a way to fetch an array or collection of SugarCRM bean objects?
That is, let's say I wanted to fetch a number of account rows that included the word Bank in their name. With raw SQL, I'd do something like this
SELECT *
FROM Accounts
WHERE name LIKE '%Associates%`;
Is there a way using the SugarCRM ORM to so something similar? If not, how do SugarCRM programmers typically handle this situation? I realize I could hack something together by selecting a list of IDs from the database
$db = DBManagerFactory::getInstance();
$result = $db->query('SELECT id FROM Accounts where name LIKE "%Banking%"');
$accounts = array();
while($row = $db->fetchRow($result))
{
$accounts[] = BeanFactory::getBean('Accounts', $row['id']);
}
but in most ORM's that would be considered inefficient, and bad practice. Is there a better way?
(Perfectly ready for the answer to be "No, there's not way to do that". I'm new to the platform and trying to get my bearings)
Rather use
$bean = BeanFactory::getBean('Accounts');
$account_list = $bean->get_full_list("", "accounts.name like '%Associates%'");
As get_list will give you what you have defined for list_max_entries_per_page.
Here is a great resource for different ways to use the standard SugarBean versus SQL: here
For your example:
$bean = BeanFactory::getBean('Accounts');
$account_list = $bean->get_list("", "accounts.name like '%Associates%'");
This is an old question but for future readers, SugarBean methods get_list() and get_full_list() seem to be deprecated and it is advised to use SugarQuery instead.
$bean = BeanFactory::getBean('Accounts');
$query = new SugarQuery();
$query->from($bean, array('team_security' => false));
$query->where()->contains('name', 'Associates');
$account_list = $query->execute();

Categories