Bulk Update in Symfony using Propel - php

How can I do a bulk update in symfony using propel when my select criteria has joins in them? Here's an example of what I am trying to do.
$conn = Propel::getConnection(BudgetLinePeer::DATABASE_NAME);
// Create a Criteria object that will select the correct rows from the database
$selectCriteria = new Criteria();
$selectCriteria->add(BudgetLinePeer::IDCOL1, $idcol1, Criteria::EQUAL);
$selectCriteria->addJoin(ProjectBudgetLinePeer::IDBUDGET_LINE, BudgetLinePeer::IDBUDGET_LINE);
$selectCriteria->add(ProjectBudgetLinePeer::IDCLIENT, $idclient, Criteria::EQUAL);
$selectCriteria->add(ProjectBudgetLinePeer::IDPROJECT, $project->getIdproject(), Criteria::EQUAL);
// Create a Criteria object includes the value you want to set
$updateCriteria = new Criteria();
$updateCriteria->add(BudgetLinePeer::STATUS, $status);
// Execute the query
BasePeer::doUpdate($selectCriteria, $updateCriteria, $conn);
I am trying to make an update(update new status) in table BudgetLine.
EDIT: Here's the snippet of the error I am getting:
Invalid argument supplied for foreach() in
/Applications/MAMP/htdocs/proj_ict_new/trunk/cbm/plugins/sfPropelORMPlugin/lib/vendor/propel/runtime/lib/util/BasePeer.php
on line 369
This is related with the join statement. I tried to use useXYZQuery()->filterCon()->endUse(). but ended up with same error again.

I was able to figure out another way to solve this problem. Here's the solution:
$budgetLine_ids = BudgetLineQuery::create()
->filterByIdcol1($col1)
->useProjectBudgetLineQuery()
->filterbyIdproject()->endUse()->find()->toArray('budgetline_id');
$selectCriteria = BudgetLineQuery::create()->filterByIdbudgetLine(array_keys($budgetLine_ids));
// Create a Criteria object includes the value you want to set
$updateCriteria = new Criteria();
$updateCriteria->add(BudgetLinePeer::STATUS, $status);
// Execute the query
BasePeer::doUpdate($selectCriteria, $updateCriteria, $conn);

Related

Get columns names when result set (stored procedure) is empty (Laravel)

I have come across a situation where the columns generated by my stored procedure are dynamic. I want to know, do we have any method to get the columns names of a result set when the result set returned by stored procedure is empty in Laravel?
I can easily access columns by iterations over the result set when it is not empty but is there any method to access the column names when the data is empty?
That's what I mean to be exact.
$data = Schema::getColumnListing("call conflictReport(123,'2016-08-01 09:00:31','2016-08-01 09:00:31')");
My procedure takes 3 parameters as in parameters.
When the result set is empty, I can't access the column names. I am using getColumnListing of Laravel.
Here's how to do it directly with PDO. With Laravel, you should be able to us the getPdo() function of your MySQLConnection object.
<?php
$pdo = new PDO('mysql:host=localhost;dbname=test', 'root', 'xxxxxxxx');
$stmt = $pdo->query('select * from mytable where false');
$colCount = $stmt->columnCount();
for ($col = 0; $col < $colCount; ++$col) {
print_r($stmt->getColumnMeta($col));
}
I tested and found this works with a CALL query too:
$stmt = $pdo->query('call myproc()');
Basically any statement with a result set has column metadata.
See PDOStatement::getColumnMeta() for more information.

Loop through all items in a table mySQL

I'm going to use this as a cronjob, I'm trying to loop through all of the rows in a table in my database, then update each item in the the row. (I update by calling another website's API to get updated information.) I connect to the database using PDO. My code:
$loop = $dbh->prepare("SELECT * FROM item_list");
$loop->execute();
while($row = mysql_fetch_array($loop)){
...get new info and update database...
}
I have error checking on and my error is: "Warning: mysql_fetch_array() expects parameter 1 to be resource, object given in page/directory.php on line 71"
I have all the code written to update each item as it loops through, I just can't get it to loop though.
You are mixing PDO and mysql extension. Use PDO only:
while ($row = $loop->fetch()) {
...
}
$loop = $dbh->prepare("SELECT * FROM item_list");
$loop->execute();
while($row = $loop->fetch()){
...get new info and update database...
}
Try this!

use php and mysql to see if a table exsists

I am working with a php program and want to check if a table exists. If it does exist, do nothing, if it doesn't create and populate the table. I ran across
if(mysql_query("DESCRIBE `table`")) {
// Exists
}
but this only takes action if it does exist. Would this
if(!mysql_query("DESCRIBE `table`")) {
// create and populate table
}
do what I am asking?
What you're doing won't work because the test is only testing the return value of the mysql_query() call, not the result of the query itself.
You need to query the tables with SHOW TABLES LIKE 'table' and check the number of rows returned:
$db = new mysqli(...);
$result = $db->query("SHOW TABLES LIKE 'table'");
if ($result->num_rows == 0) {
// create table
}
Note: mysql_*() is deprecated - you shouldn't use it for new code.
You'll find the MySQL reference here, and the PHP reference for mysqli here

Can raw SQL be parsed into a Zend\Db\Sql\Select object?

is there a way to parse a SQL statement string into a Zend\Db\Sql\Select object?
something along the lines of:
$sql = 'SELECT * FROM table WHERE col1 = "x"';
$select = new Select();
$select->parseSql($sql);
I have many uses for this, basically maintaining the use of a Select object when executing a raw SQL statement, for example to pass to Zend\Paginator\Adapter\DbSelect without having to write a custom Zend\Paginator\Adapter\DbSelect Class.
got one answer through a twitter response, specific to the Paginator use case:
$resultSet = iterator_to_array($adapter->query($sql_statement)->execute());
$iteratorAdapter = new ArrayAdapter($resultSet);
$paginator = new Paginator($iteratorAdapter);

how to add additional select columns in symfony 1.0

How can I add an additional column to my select query so when I execute TableA::doSelect($c) I could also get some columns of TableB? It does not seem that addSelectColumn is available in symfony 1.0, unless I'm missing something.
$c = new Criteria();
$c->setDistinct();
// need to add TableB::START_DATE to select
$c->addJoin(TableA::ID, TableB::ID);
...
$c->addAscendingOrderByColumn ( TableB::START_DATE );
$result = TableA::doSelect($c);
You could use addSelectColumn() with Symfony 1.0 but should avoid adding doSelect() in such cases.
This method is executing an object and expects to get all its fields in the same order they are described in the schema file. Trying to skip a field or add one from another table will be ignored in the result set.
Depending on your Propel version you could try and use the doSelectStmt(), doSelectRS() or doSelectJoin() methods instead.
Or go with the easiest way and just use a prepareStatement():
$con = Propel::getConnection();
$sql = "SELECT `table1`.field1, `table2`.field2 FROM `table1` JOIN `table2`. ON ..."
$st = $con->prepareStatement($sql);
$rs = $st->executeQuery();
while ($rs->next())
{
...
}
http://symfony.com/legacy/doc/cookbook/1_0/en/behaviors

Categories