Is it possible to 'prepare' a Propel 1.4 Criteria so it can be used efficiently inside a loop? - prepared-statement

I'm using symfony 1.3 with Propel 1.4. I need to use a prepared query that will be used inside a loop changing the values of bound parameters.
Is it possible to do this with Propel 1.4? I want to retrieve results as objects so don't want to use raw SQL if I can help it.

Propel does not seem to support this out of the box. Take one of the examples, the Book class. BookPeer::doSelect() calls BookPeer::doSelectStmt() and passes the result to BookPeer::populateObjects(). BookPeer::doSelectStmt() calls BasePeer::doSelect(), which always calls BasePeer::createSelectSql() to create the SQL statement, prepares it, and passes it to BasePeer::populateStmtValues() to actually bind the values.
You can take code from these methods to get something like this (without exception or transaction handling):
$criteria = new Criteria();
$criteria->addThis();
$criteria->addThat();
$con = Propel::getConnection($criteria->getDbName(), Propel::CONNECTION_READ);
$params = array();
$sql = BasePeer::createSelectSql($criteria, $params);
$stmt = $con->prepare($sql);
// $stmt is a prepared PDOStatement
// $params is an array of the form: [{'table': 'tableName', 'column': 'columnName', 'value': 'paramValue'}, ...]
// You can keep this format and use BasePeer::populateStmtValues(), or you can call $stmt->bindParam() yourself
// Now in the loop, we set the params ourself
foreach ($loop as $loopData) {
// Bind the params, using your preferred method
// ...
// Pass the prepared and bound statement to populateObjects()
$books = BookPeer::populateObjects($stmt);
// Do something with the found objects
// ...
}

Related

Can I use same param name multiple times in the URL for codeigniter-restserver?

http://example.com/api/transfer/transfers/code/456/code/234
When using $this->get('code') on a url like above I expect the REST library to return an array or a list of the codes.
Instead it returns the last one.
Is there a way to return both values in a list, or is there another recommandation for formating the URL.
Thank you
I know it has been long since you posted the question. However it could help other people looking for the same thing.
Assuming that transfer is your controller and transfers is the function, another way to format your url could be:
http://example.com/api/transfer/transfers?code[]=456&code[]=234
This was you perform $this->get('code') you'll get an array back.
If you are creating the url via code then you may use http_build_query(). It handles the necessary escaping. It means it will replace [ for %5B and ] for %5D, in this case.
The code would be like:
$codes = array(456, 234);
$query = http_build_query(array('code' => $data));
$url = 'http://example.com/api/transfer/transfers?' . $query;

Sql in Drupal 7 Tpl file

Can any one suggest me how to write SQL query in .tpl file?
I tried to write db_query("select * from table_name "); But it is not working.
Please give me a solution to write sql query in tpl file
.tpl files are for the template layout and should not have much more logic than if a region has content. For anything requiring more logic I'd recommend either adding an applicable hook in your template.php or look into creating your own module.
What is the goal you are trying to achieve it may be solvable in another way instead of using a custom query. Likely you can create a view for your page?
Otherwise, try testing your query first through your database - if you have phpmyadmin or similar set up to see if it is actually returning a result.
And see https://api.drupal.org/api/drupal/includes!database!database.inc/function/db_query/7 for more information on how to use that particular function.
I have tried below code and getting the records
$result = db_query("SELECT * from node");
$records = $result->fetchAll();
print_r($records);
Can you check the tpl file is correct or not, OR can you share the code.
Better to write a function in your module and return the data from the query to the tpl file.
// function inside your module
function your_custom_function(){
$result = db_query("SELECT * from node");
$records = $result->fetchAll();
retun $records;
}
Then you can call this function from tpl file
// calling the function from tpl
$data = your_custom_function();
foreach($data as $value){
// do your template logic here
}

Change Value of ParamInfo after adding to DynamicParameters?

I'm calling a stored proc in a foreach loop and would like to change the value of one of the parameters on each iteration. Currently, there doesn't seem to be any way to access the parameters once they've been added to DynamicParameters although from reading the source, I can see that DynamicParameters does keep an internal Dictionary. Any reason why this isn't public or if there's another way to get at the ParamInfos to change values?
Update
What I have currently:
foreach ( var fooID in fooIDs )
{
var dynamicParameters = new DynamicParameters();
dynamicParameters.Add( ParameterNames.BarID, barID );
dynamicParameters.Add( ParameterNames.FooID, fooID);
connection.Execute( ProcNames.MyProc, dynamicParameters, commandType:CommandType.StoredProcedure );
}
Re-Add the parameter.
// Call Add() with new values.
dynamicParameters.Add(ParameterNames.BarID, differentBarID);
There is no real reason DynamicParameters is so secret about what it does, the ParamInfo class could be exposed and I would be happy to provide proper iteration/modification properties and/or methods. If you feel like you would like to pitch in, please submit a patch.
In the mean time you can simply implement IDynamicParameters which is the trivial interface we use to dispatch this to the underlying command, in your app. You can use DynamicParameters as a starting point.

how to force drupal function to not use DB cache?

i have a module and i am using node_load(array('nid' => arg(1)));
now the problem is that this function keep getting its data for node_load from DB cache.
how can i force this function to not use DB cache?
Example
my link is http://mydomain.com/node/344983
now:
$node=node_load(array('nid'=>arg(1)),null,true);
echo $node->nid . " -- " arg(1);
output
435632 -- 435632
which is a randomly node id (available on the system)
and everytime i ctrl+F5 my browser i get new nid!!
Thanks for your help
Where are you calling this? For example, are you using it as part of your template.php file, as part of a page, or as an external module?
Unless you have this wrapped in a function with its own namespace, try naming the variable differently than $node -- for example, name it $my_node. Depending on the context, the 'node' name is very likely to be accessed and modified by Drupal core and other modules.
If this is happening inside of a function, try the following and let me know what the output is:
$test_node_1 = node_load(344983); // Any hard-coded $nid that actually exists
echo $test_node_1->nid;
$test_node_2 = node_load(arg(1)); // Consider using hook_menu loaders instead of arg() in the future, but that's another discussion
echo $test_node_2->nid;
$test_node_3 = menu_get_object(); // Another method that is better than arg()
echo $test_node_3->nid;
Edit:
Since you're using hook_block, I think I see your problem -- the block itself is being cached, not the node.
Try setting BLOCK_NO_CACHE or BLOCK_CACHE_PER_PAGE in hook_block, per the documentation at http://api.drupal.org/api/drupal/developer--hooks--core.php/function/hook_block/6
You should also try to avoid arg() whenever possible -- it's a little bit of a security risk, and there are better ways to accomplish just about anything arg() would do in a module environment.
Edit:*
Some sample code that shows what I'm referring to:
function foo_block ($op = 'list', $delta = 0, $edit = array()) {
switch ($op) {
case 'list':
$blocks[0] = array(
'info' => 'I am a block!',
'status' => 1,
'cache' => BLOCK_NO_CACHE // Add this line
);
return $block;
case 'view':
.....
}
}
node_load uses db_query, which uses mysql_query -- so there's no way to easily change the database's cache through that function.
But, node_load does use Drupal's static $nodes cache -- It's possible that this is your problem instead of the database's cache. You can have node_load clear that cache by calling node_load with $reset = TRUE (node_load($nid, NULL, TRUE).
Full documentation is on the node_load manual page at http://api.drupal.org/api/drupal/modules--node--node.module/function/node_load/6
I have had luck passing in the node id to node_load not in an array.
node_load(1);
According to Druapl's api this is acceptable and it looks like if you pass in an array as the first variable it's loaded as an array of conditions to match against in the database query.
The issue is not with arg(), your issue is that you have caching enabled for anonymous users.
You can switch off caching, or you can exclude your module's menu items from the cache with the cache exclude module.
edit: As you've now explained that this is a block, you can use BLOCK_NO_CACHE in hook_block to exclude your block from the block cache.

Doctrine wildcard WHERE query?

Is it possible to craft a wildcard WHERE query with Doctrine, like so:
->where('this_field = ?', ANYTHING);
I'd be using this to build a search query dynamically out of unknown user-selected parameters and therefore would need to have the condition in place but capable of accepting the value "anything".
To add, I can get this to work:
$field = 'this_field = ?';
$value = 5;
...
->where($field, $value);
... but it's still doesn't allow me to use "anything" as value or to eliminate the entire query condition. The following fails:
$field = NULL;
$value = NULL;
...
->where($field, $value);
Thanks
If you build the query dynamically you can also check wther $value has a value or not and then add the where part if necessary. E.g.
$q; // Your query object.
if(isset($value)) { // or empty() or maybe just if($value) depending on your needs.
$q->where('this_field = ?', $value);
}
This is easier to understand and easier to debug imo.
[Answer to own question]
After playing around with this, found a solution that works, posting here for others.
Using ->whereIn allows Doctrine to ignore a query condition completely:
$empty = array();
...
->andWhereIn('this_field', $empty)
The above condition doesn't get included in the resulting SQL at all.

Resources