Yii createCommand not working - yii

I use Yii MVC to develop code, and I have a code, that does not work;
I can't find the error, maybe you guys can;
$sql = "
select extension
from file_extension
where status = :status and extension in ('" . $extensions . "');
";
$status = FileExtension::ACTIVE_STATUS;
$cmd = Yii::app()->getDb()->createCommand($sql);
$cmd->bindParam(":status", $status, PDO::PARAM_INT);
$arrObj = $cmd->queryAll();
when I use print_r($arrObj); i get array()
Why don't I get results?
after some work, I see that my query is like:
select extension
from file_extension
where status = :status and extension in ('gif
','pdf
','chm
');
and because of the line breakes and the empty spaces, my sql query fails;
what can i do to obtain:
select extension
from file_extension
where status = :status and extension in ('gif','pdf','chm');

Try:
$extensionList = Yii::app()->db->createCommand()->select('extension')
->from('file_extension AS fe')
->where("fe.status = :status AND fe.extension IN ('gif','pdf','chm')", array(':status' => FileExtension::ACTIVE_STATUS))
->queryAll();

Related

Magento Soap api get all products sku, names, short description and image

My code is working fine but take so much time for show result because i have above 1500 products.
So any one modify my code or best way to show results
$results = $proxy->catalogProductList($sessionId);
$productData = new stdClass();
$productData->additional_attributes = array('short_description','cost');
foreach($results as $value){
$results_product = $proxy->catalogProductInfo($sessionId,$value->product_id,null,$productData);
$pro_imag = $proxy->catalogProductAttributeMediaList($sessionId, $value->product_id);
echo "";
echo "".$sno++."".$value->product_id."".$value->sku."".$value->name."".$results_product->additional_attributes[0]->value."".abs($results_product->additional_attributes[1]->value)."".abs($results_product->price)." url."' width='80px' height='80px'> ";
echo "";
}
Try the following code and use magento site for easy api examples like following, example SOAP V2 (Complex Filter)
<?php
$client = new SoapClient('http://magentohost/api/v2_soap/?wsdl');
$session = $client->login('apiUser', 'apiKey');
$complexFilter = array(
'complex_filter' => array(
array(
'key' => 'type',
'value' => array('key' => 'in', 'value' => 'simple,configurable')
)
)
);
$result = $client->catalogProductList($session, $complexFilter);
var_dump ($result);

PDO variable not capturing/passing value

This code works but it is one change short of proper PDO--I would need to change " $user_ID " in the where clause to " :user_ID ".
HOWEVER, when I do that, the code doesn't update the contributions by 3 (which is the goal of the code). But it does work and update contributions as long as I don't change $user_ID to :user_ID.
Can anyone see why and what I'm doing wrong from this?
$yesupdate = "UPDATE points SET contributions = contributions + 3 WHERE ID
= $user_ID";
$stmt4 = $dbh->prepare($yesupdate);
$stmt4->bindParam(':user_ID', $user_ID, PDO::PARAM_INT);
$stmt4->bindParam(':WID', $yes_WID, PDO::PARAM_INT);
$stmt4->bindParam(':approved', $e = Y, PDO::PARAM_STR);
$stmt4->bindParam(':position', $row2[0]['position'], PDO::PARAM_INT);
$stmt4->execute();
FYI, the above code is a second query that follows the below query:
$yesupdate = "UPDATE writing SET approved = :approved, position = :position
WHERE WID = :WID";
$stmt2 = $dbh->prepare($yesupdate);
$stmt2->bindParam(':WID', $yes_WID, PDO::PARAM_INT);
$stmt2->bindParam(':approved', $e = Y, PDO::PARAM_STR);
$stmt2->bindParam(':position', $row2[0]['position'], PDO::PARAM_INT);
$stmt2->execute();

Codeigniter active record where array

I am using codeigniter and active record. I am selecting my data over WHERE with array. Any like this. But how can I insert tags '>' and '<'? It is possible?
$whereQuery['service.service_end_date'] = $start;
Thank you for replies.
This might be what you want:
Associative array method:
$array = array('name' => $name, 'title' => $title, 'status' => $status);
$this->db->where($array);
// Produces: WHERE name = 'Joe' AND title = 'boss' AND status = 'active'
You can include your own operators using this method as well:
$array = array('name !=' => $name, 'id <' => $id, 'date >' => $date);
$this->db->where($array);
Source:
http://ellislab.com/codeigniter/user-guide/database/active_record.html
http://ellislab.com/codeigniter/user-guide/database/active_record.html
$whereQuery['service.service_end_date >'] = $start;
$whereQuery['service.service_end_date <'] = $start;
You can pass > < <> in CI where function
$this->db->where('field_name <', "Condition_value");
From Codeigniter page :
You can include an operator in the first parameter in order to control the comparison:
$this->db->where('name !=', $name);
$this->db->where('id <', $id);
// Produces: WHERE name != 'Joe' AND id < 45

Yii update with join

I have a code
$command = Yii::app()->db->createCommand()
->update(
'queue q',
array('i.status_id' => $status_id)
)
->join('item i', 'q.item_id = i.item_id')
->where('IN', 'queue_id', $ids);
after I call $command->buildQuery() I get an error:
CDbCommand failed to execute the SQL statement: Invalid parameter number: parameter was not defined. The SQL statement executed was: UPDATE queue q SET i.status_id=:i.status_id
The impression is that it does not see the join and where commands.
What the problem?
Your code is valid with the newest Yii version. This MySQL-specific functionality has been added as of 1.1.14: https://github.com/yiisoft/yii/commit/ed49b77ca059c0895be17df5813ee1e83d4c916d.
The where clause should be in the update() function like this
Yii::app()->db->createCommand()
->update(
'queue q',
array('i.status_id' => $status_id),array('in', 'queue_id', $ids)
);
And regarding the JOIN part there is a open bug at https://github.com/yiisoft/yii/issues/124 (Im not sure. Correct me if Im wrong). Please let me know if there is a workaround.
You have to bind the parameters:
$command = Yii::app()->db->createCommand()
->update(
'queue q',
array('i.status_id' => ':status_id'),
array('in', 'queue_id', $ids),
array(':status_id' => $status_id),
)
->join('item i', 'q.item_id = i.item_id');
Having come across this problem a few times in my projects I have come-up with the following Yii work-around using CDbCriteria which is a little hacky, but gives the security of param count matching.
When applied to your example my code would be (guessing a little bit of your structure):
$ids = array(1,2,3,4,5);
$criteria = new CDbCriteria();
$criteria->addInCondition('i.queue_id',$ids);
$sql = '
UPDATE queue q
JOIN item i
ON q.item_id = i.item_id
SET i.status_id = :status
WHERE '.$criteria->condition;
$command = Yii::app()->db->createCommand($sql);
$command->bindValue('status',$status);
$command->bindValues($criteria->params);
$rows = $command->execute();

Zend Framework SQL update query

How would I write this SQL the Zend Framework way?
UPDATE register
SET balance = (balance + 10)
WHERE added_date > 1259944184 ;
I can't find any examples of this on Zend's website or the web.
Do I need to use "Zend_Db_Expr"?
This worked for me:
$data = array(balance => new Zend_DB_Expr('balance + 10'));
$db->update('register ', $data, 'added_date > 1259944184');
acording to zend framwork documentation
use this
$data = array(
'balance' => 'balance + 10'
);
$n = $db->update('register ', $data, 'added_date > 1259944184');
Try this... make sure your model is ready.
$table = new register();
This is the model class
balance=balance+10;
$data = array(
'balance' => 'balance'
);
$where = $table->getAdapter()->quoteInto('added_date > '. 1259944184 );
You can use $where[] for multiple conditions
$table->update($data, $where);
For more details follow link
I used this to modify element order to top.
Code as follow from the table class extending Zend_Db_Table_Abstract:
$data = array('i_order' => new Zend_DB_Expr('i_order + 1'));
return $this->getAdapter()->update($this->_name, $data, "i_id != {$oCurrent->i_id} AND i_order < {$i_order}");