wordpress wp query posts & meta value - sql

I have a cpt "members".
In a post I can select multiple members (object) via ACF in the specific post.
Now I want say: Please give me all posts where a member (e.g. nr. 76) ist marked in the post.
I try the whole day, and have no idea.
It's possible if i change the acf field to one number to get the correct posts, but Its important that I can select multiple members.
Thanks for helping!

Finally got it :-)
$args = array(
'post_type'=>'post',
'numberposts'=> -1,
'meta_query' => array(
array(
'key' => 'members',
'value' => '76', //example
'compare'=>'LIKE'
)
)
);

Related

Jenssegers / select('column') returns more columns than the one specified in the select

I have the following issue with jenssegers query builder (I'm a new user):
print_r(DB::table($tablename)->where('_id',$_id)->select($table_structure_record['field'])->get());
returns me more than one column, despite only one column is specified in the select statement:
Array ( [0] => Array ( [_id] => MongoDB\BSON\ObjectID Object ( [oid] => 5780b81d93f7fb0e00d0f252 ) [collection] => structure ) )
My expected result would be only ([collection] => structure) , I don't understand why i also get "[_id] => MongoDB\BSON\ObjectID Object ( [oid] => 5780b81d93f7fb0e00d0f252 )"
Can someone help me ? Despite many searches, it seems select statement is supposed to return ONLY the columns specified, not any other !
MongoDb always returns the _id field unless you specifically set it not to while making the request(MongoDb Limit Fields to Return from Query Documentation).
You can try using project instead.Then it will be something like this:
DB::table($tablename)->where('_id',$_id)->project([$table_structure_record['field'] => 1, "_id" => 0])->get());

Add AS-Expression to selected columns in getCollection()

As an follow up idea to solve my "sort product collection by sub-category" problem explained in my question sorting collections by subcategory, an attribute and by productname I had the idea to add a AS expression to the selected fields of the collection.
What I want to achieve (SQL-wise) is something like this:
SELECT field1, 'some string' as 'category_name' FROM ...
So I thought I could use the addExpressionFieldToSelect in the method chain to dynamically add the category name string to the products collection.
For that have the following:
// ...
$_products = Mage::getModel('catalog/product')
->getCollection()
->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left')
->addAttributeToSelect('*')
->addExpressionFieldToSelect('\'some category name\'', 'AS', 'category_name')
->addAttributeToFilter('status', 1)
->addAttributeToFilter('visibility', 4)
->addAttributeToFilter('is_saleable', array('like' => '1'))
->addAttributeToFilter('category_id', $_subcategory_finset_ids)
->addAttributeToSort('ws_geschmack', 'ASC')
->addAttributeToSort('name', 'ASC');
// ...
But with that I get an error:
Fatal error: Call to undefined method Mage_Catalog_Model_Resource_Product_Collection::addExpressionFieldToSelect() ...
Short explanation: In fact I am not able to sort product collections by sub-category (string) I first query all child categories of a parent category (ordered) and within a loop I query all products of these child categories (down to the deepest level) to get products sublists of each subcategory and sub-sub categories. To fully understand my problem, please refer to my question mentioned above.
It is just a try, I do not know if all this is really working. As a hack for now I just try to come a little closer with that AS expression field holding the category name of the outer loop. I then could merge these collections and would have a final products collection.
If there is a more simple way, please let me know.
Any help is greatly appreciated!!
Update
Instead of using addExpressionFieldToSelect it is possible to use this:
$_products->getSelect()
->columns(
array(
'category_name' => new Zend_Db_Expr(
'some string')
)
);
As stated above in my update, I solved the problem by using:
$_products->getSelect()
->columns(
array(
'category_name' => new Zend_Db_Expr('some string')
)
);
In fact, simple solution!

find all using distinct query in Cakephp

I am newbie in cakephp and trying to implement this query
SELECT DISTINCT mobilenO,DATETIME
FROM textmessage;
what i am trying right now is this
$this->find('all',array(
'fields'=>array('Message.dateTime', 'DISTINCT Message.mobileNo'),
'order'=>'Message.idTextMessage DESC',
'conditions' => array('Message.User_id' => $userid)));
It is throwing me errors. I actually want to select only distinct or unique numbers from db. I have added the distinct in my query, but it didnt work.
i have to use distinct first not after 1st column.. dont know why but it works
$this->find('all',array(
'fields'=>array('DISTINCT mobileNo','dateTime'),
'order'=>'Message.idTextMessage DESC',
'conditions' => array('Message.User_id' => $userid)));

ClistView - Parent and Child on single view

I have two table: 'Streams' and 'Punches'. One stream have many punches.
In my view, I use 'CListView' to show all punch's image of a stream.
But I want show the stream's image in first page, and all punch's image will show on next pages.
How can I do that? thanks...
Ah, I resolved this problem... Instend of using CActiveDataProvider, I use CSqlDataProvider to "union" two table.
$count=Yii::app()->db->createCommand('SELECT COUNT(*) FROM tbl_user')->queryScalar();
$sql='SELECT * FROM streams union SELECT * FROM punches';
$dataProvider=new CSqlDataProvider($sql, array(
'totalItemCount'=>$count,
'sort'=>array(
'attributes'=>array(
'id', 'username', 'email',
),
),
'pagination'=>array(
'pageSize'=>10,
),
));
// $dataProvider->getData() will return a list of arrays.

CGRIDVIEW TWO TABLES DATA DISPLAY IN YII

i have 2 tables (STUDENT AND EMPLOYEE)
student(reg_no , s_name, dept, f_name) reg_no is primary key
employee(e_no,design,salary,reg_no) reg_no is foreign key reference student(reg_no).
i want to display e_no,design from employee
and s_name,dept from student table.
so my complete cgridview would be
e_no, design,s_name,dept
my code for employee/admin.php
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'employee-grid',
'dataProvider'=>$model->searchEmployees(),
//'filter'=>$model,
'columns'=>array(
'e_no',
'e_name',
'design',
'salary',
'reg_no',
array('name'=>'student.s_name', 'value'=>'$data->student->s_name'), // student name
'salary', // employee.salary
array(
'class'=>'CButtonColumn',
),
),
));
?>
my code for model/employee.php for searchEmployees()
public function searchEmployees()
{
$criteria=new CDbCriteria;
$criteria->alias = 'i';
$criteria->compare('e_no',$this->e_no);
$criteria->compare('e_name',$this->e_name,true);
$criteria->compare('design',$this->design,true);
$criteria->compare('salary',$this->salary);
$criteria->compare('reg_no',$this->reg_no);
$criteria->join= 'JOIN student d ON (i.reg_no=d.reg_no)';
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'sort'=>array(
'defaultOrder'=>'reg_no ASC',
),
));
}
BUT I FACE ERROR
Property "Employee.studentname" is not defined.
HOW TO SOLVE THE ERROR PLZ HELP.
THANKS
You can configure the cgridview to display information for related records using CActiveRecord->getRelated in your customization of columns... it'd be as follows:
student(reg_no , s_name, dept, f_name) would be model Student.
employee(e_no,design,salary,reg_no) would be model Employee.
Employee.e_no, Employee.design, Student.s_name and Student.dept are your desired values so your columns array should be:
<?php
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'student-grid',
'dataProvider'=>Student::model()->search(),
//'filter'=>$model,
'columns'=>array(
array(
'header'=>Employee::model()->getAttributeLabel('e_no'), //column header
'value'=>'$data->getRelated('employee')->e_no', //column name, php expression
'type'=>'raw',
),
array(
'header'=>Employee::model()->getAttributeLabel(\'design\'), //column header
'value'=>'$data->getRelated(\'employee\')->design', //column name, php expression
'type'=>'raw',
),
's_name',
'dept',
),
));
?>
Notice how related record data is done in the 'value' specification of a custom column, and that the argument to getRelated() is the name of the relationship defined to the Employee model defined in the Student->relations() method. Also check CGridView documentation on columns for more info on how this works.
Update: You can easily switch the approach of this CGridView instantiation to use an Employee instead of a Student and still display the same info by changing the columns array.
Update 2: You can easily handle non existing relationships in the columns array:
'columns'=>array(
array(
'header'=>Employee::model()->getAttributeLabel('e_no'), //column header
'value'=>'($data->getRelated(\'employee\')=== null)?'No related employee registry':$data->getRelated('employee')->e_no', //column name, php expression
'type'=>'raw',
),
array(
'header'=>Employee::model()->getAttributeLabel(\'design\'), //column header
'value'=>'($data->getRelated(\'employee\')=== null)?'No related employee registry':$data->getRelated(\'employee\')->design', //column name, php expression
'type'=>'raw',
),
's_name',
'dept',
),
With this tweaks in the 'value' fields of our members in 'columns' we display a small message whenever the model tries to access a related model. getRelated() returns the related record(s) or null if none is found. Maybe you have Students an equivalent Employee model, which might be the cause of the error. I hope the above helps you resolve it.