How to use where clause on the yii? - yii

I need to use where clause on yii, below of my code, I need to add where activated == 0 && send_email == 0.
$model = User::model()->findAll();
Thanks

There are multiple ways to do this:
$model = User::model()->findAll('activated=0 AND send_email=0');
Or,
$model = User::model()->findAll('activated=:activated And send_email=:sendEmail',
array(':activated'=>0,':sendEmail'=> 0));
Or,
$criteria = new CDbCriteria;
$criteria->condition='activated=:activated AND send_email=:sendEmail';
$criteria->params=array(':activated'=>0,':sendEmail'=>0);
$model=User::model()->findAll($criteria);
Or,
$model = User::model()->findAllByAttributes(array('activated'=>0, 'send_email'=>0));
Or,
$model = User::model()->findAllBySql('SELECT * FROM user WHERE activated=:activated AND send_email=:sendEmail', array(':activated'=>0, 'sendEmail'=>0));
More info here. Hope that helps :)

Simply try this:
$model = User::model()->findAll(array("condition"=> "activated=0 AND send_email=0"));

Related

How to do cusotm db query in controller? Zend 2

I wanto to sipmly iaterate the result in view. Problem is that it do not accually work. What is missing? I read the documentatnion but they do not give a full ansver how to retrieve data by cusotm query. For me as a beinnger is hard to understend what do next ? I mean about this query I know how to pass it to view by viewmodel et. Please Help.
$config = $this->getServiceLocator()->get('config');
$adapter = new \Zend\Db\Adapter\Adapter($config[db]);
$sql = new Sql($adapter);
$select = $sql->select();
$select->from('brokerzy');
$select->where(array('broker_status' => 'publish'));
$stm = $sql->prepareStatementForSqlObject($select);
$results = $stm->execute();
First, you probably shouldn't be doing queries in your controller, that's bad practice. These queries should be moved behind a service interface.
However, to answer your question (if I understand it), to pass the results of your query to a view, you should use a view model. It would look something like this.
//Your Code
$results = $stm->execute();
$viewModel = new \Zend\View\Model\ViewModel();
$viewModel->results = $results;
return $viewModel;

Yii critetia addCondition and addInCondition + and/or operators

Is it possible in a criteria to have AND & OR Operators between addCondition and addInCondition? I have something like this:
$criteria = new CDbCriteria;
//if value is TRUE
$criteria->addCondition('t.value = TRUE');
//if value is False
$criteria->addCondition('t.value = FALSE');
$criteria->addInCondition('t.id',$array);
I don't know where I will put the operators. Thanks.
According the class reference (http://www.yiiframework.com/doc/api/1.1/CDbCriteria#addInCondition-detail':
public CDbCriteria addInCondition(string $column, array $values, string $operator='AND')
The third parameter is the operator which will be added to the existing one, so for you it will be:
$criteria->addInCondition('t.id',$array, 'OR');

Empty response when making a Select with 2 where clauses

My function of XMLRPC works like this, check if content already exists at my website, if it is up-to-date and update it if necessary.
To do it I check for it´s number ( both content types can have same number) it´s hash (unique) and type.
Before I had only one kind of content, and my function was working properly , but now I need to check also content type instead of only number and hash.
that is the problem, whem I try to do it, I got an empty anwser. My Function now is like this:
$query = db_query("SELECT (table_num.field_num) FROM (table_num, table_type) WHERE table_num.entity = table_type.entity AND table_num.field_num = :num AND table_type.field_type = :type", array(':num' => $num, ':type' =>$type));
foreach ($query as $record) {
if($record->rowCount()==0) return 0;
else {
$query = db_query("SELECT (table_hash.field_hash) FROM (table_hash, table_type) WHERE table_hash.entity = table_type.entity AND table_hash.field_hash = :hash AND table_type.field_type = :type", array(':hash' => $hash, ':type' =>$type));
foreach ($query as $record) {
if($record->rowCount()==1) return 1;
else{
$query = db_query("SELECT (table_num.entity_id) FROM (table_num, table_type) WHERE table_num.entity = table_type.entity AND table_num.field_num = :num AND table_type.field_type = :type", array(':num' => $num, ':type' =>$type));
foreach ($query as $record) {
echo $record->field_entity_id;
}
return $record;
P.S. when it returns 0 it does not exists in my DB, 1 is when it´s up to date and entity when I need to update it.
Sorry for long SQL but I just start to use it, and I didn´t manage to get it shorter.
Does someone have any idea why I am getting an empty anwser?
The way your building your queries is dangerous and lead to SQL injection as your not escaping your variables.
Instead of:
$query = db_query("SELECT (table_num.field_num) FROM table_num WHERE table_num.field_num = $num AND table_type.field_type = $type");
You can use:
$query = db_query("SELECT (table_num.field_num) FROM table_num WHERE table_num.field_num = :num AND table_type.field_type = :type", array(':num' => $num, ':type' =>$type));
This way Drupal will sanitize your variables before inserting them so you know it won't lead to any vulnerabilities.
I don't actually fully understand what your question is asking - if you could clarify it a bit I'll see if I can help with that too...

looking for next ID before save in yii

I'm trying to get the next product ID, but it says that I need to specify criteria... CDbCriteria. I searched around, I don't see other people using criteria so I must be doing something wrong?? I am looking for the next product_id.
public function getNextID()
{
$model = new Product();
$last_ID= $model->findByAttributes(array('product_id'=>'product_id DESC'));
$next_ID=$last_ID+1;
return $next_ID;
}
Do not quite understand what to know before you insert id, but try to do so
$criteria = new CDbCriteria();
$criteria->select = 'id';
$criteria->order = 'id DESC';
$criteria->limit = 1;
$product = Product::model()->find($criteria);
// id of last row
$lastId = $product->id;

Propel to Doctrine Code Snippet

I am using symfony 1.4 with Doctrine ORM. I am editing some of the actions, and I need to rewrite a Propel query into Doctrine. Here's the snippet:
$c = new Criteria();
$c->add(BlogCommentPeer::BLOG_POST_ID, $request->getParameter('id'));
$c->addAscendingOrderByColumn(BlogCommentPeer::CREATED_AT);
$this->comments = BlogCommentPeer::doSelect($c);
Can anyone help with the conversion? Thanks.
In your BlogCommentTable.php file, put this method :
public functoion retrieveByPostId($post_id)
{
$q = $this->createQuery('c')
->where('c.blog_post_id = ?', array($post_id))
->orderBy('c.created_at ASC');
return $q->execute();
}
And in your action:
$this->comments = Doctrine_Core::getTable('BlogComment')->retrieveByPostId($request->getParameter('id'));