Yii dropDownList nested conditions (not nested dropDown) - yii

in a dropdown I would like to find data in 2 levels. Maybe my logic is wrong, but as I remember I have done such things before, the only difference was that I got always 1 simple result back, but now, I should handle an array. Here's my code:
echo $form->dropDownList($model, 'szeriaGyartmanyId', GxHtml::listDataEx(
SzeriaGyartmany::model()->findAllAttributes(
null, true, 'rajz_osszetett_technologia_id IN (:rajz_osszetett_technologia_id) AND keszDb<db', array(
':rajz_osszetett_technologia_id' => RajzOsszetettTechnologia::model()->findAllAttributes(
null, true, 'osszetett_technologia_id = :osszetett_technologia_id', array(
':osszetett_technologia_id' => OsszetettTechnologia::model()->find("name='Horganyzás alatt'")->id
)
)->id
)
)
), array('style' => 'width: auto', 'prompt' => ''));
the core gives back one single ID, it's no problem, but the second level gives back an array (or array of objects? I'm not sure). The point is, is it possible here somehow to implode resulting rajz_osszetett_technologia_ids, or do I have to do completely differently? I've tried to implode it right in place, but I got an error: Argument must be an array. So that's why I guess the result is an array of objects.
Is it clear what I would like to achieve? For me it seems kinda obvious to do it somehow like this, but maybe my logic is completely wrong. Can somebody please point me to the right direction?
Thanks a lot!
BR
c

GxActiveRecord::findAllAttributes(null,true..) returns an array of objects with only the required properties set. In order to obtain an array of ids as required you need to wrap it in GxHtml::listDataEx() and then use array_keys to obtain only the keys.
echo $form->dropDownList($model, 'szeriaGyartmanyId', GxHtml::listDataEx(
....
':rajz_osszetett_technologia_id' => implode(',',
array_keys(
GxHtml::listDataEx(
RajzOsszetettTechnologia::model()->findAllAttributes(
....
)
)
)
)
....
)
Perhaps a custom query would be easier and clearer than this.

Related

How to get the org_id from a webhook?

When a Webhook is triggered, is there a way to get the org_id from which it was fired? (Aside from climbing up the triggered item)
The only solution I found so far is:
PodioItem::get($item_id); to get the space_id
PodioSpace::get($space_id); to get the full
PodioOrganization::get_for_url($attributes = array()); I get the org_id.
See the "Bundling responses using fields parameter" section at the very bottom of https://developers.podio.com/index/api on how you can use the fields query parameter to include more data. There's even an example that goes almost all the way for you (it walks up to the space level, but you can just tack the org onto it):
/item/{item_id}?fields=app.view(full).fields(space.view(full))
For podio-php you can do:
$item = PodioItem::get($item_id, array('fields' => "app.view(full).fields(space.view(full))"));
Use PodioItem::filter instead of PodioItem::get, I'm pretty sure that you'll have the expected results, so try this:
$item = PodioItem::filter($item_id, array('filters' => "app.view(full).fields(space.view(full))"));
Hope it helps!

Yii CHtml::dropDownList. Same value under different labels

CHtml::dropDownList('name','select',$listData,$htmlOptions);
Everything is OK until I faced one issue. I've got an array, which looks like this:
array(
array('ua', 'Ukraine', '380'),
array('ru', 'Russia', '7'),
...
array('kz', 'Kazakhstan', '7'),
);
$listData is an array of (value=>label). First I walked through array and made (code=>country) array as $listData. But I found that different countries may have the same code. I can use first "two letter geo" as key, and $listData will be an unique array.
And what if I need the same value but under different labels?
It seems that the only Yii solution is to concatenate labels under one key(value).
Or use pure html and echo each option separate.
I think the sensible way to do this is with a new model, with a new id (INTEGER PRIMARY KEY AUTOINCREMENT), and hold the data in separate fields (country, geo, code.)
Create your listdata to be an array using only the new id and the country so the array looks something like this:
array( 0=>'Ukraine', 1=>'Russia', 2=>'Kazakhstan' );
This way you will only send the id, and figure out the data (geo, code, country) on the receiver side.
But I must ask about this:
It seems that the only Yii solution is to concatenate labels under one key(value).
How do you want a option-tag to look like exactly?
<option value="ua" code="380">Ukraine</option> // Like this?
Then you should set htmlOptions similar to this:
array(
'ua'=>array('code'=>'380'),
'ru'=>array('code'=>'7'),
...
);

How to combine where statements in Zend Framework 2

That may be a silly question, but I just can't find any answer to it.
I have a simple query like 'SELECT * FROM table WHERE (x=a AND y=b) OR z=c' and I just can't find out how to implement that in ZF2.
I found a lot of information on Predicates and Where objects, but I can't find out how to combine that information into a query consisting of AND and OR.
I'd really appreciate it if someone can point me to the right direction.
If you want to add them using AND, you can simply pass them into the select object using an array, for example:
$select->where(array(
'type' => 'test,
'some_field' => '1'
));
If you want more complex Where queries, then you can use a Where object to build up a query:
$where = new \Zend\Db\Sql\Where();
$where->addPredicate(
new \Zend\Db\Sql\Predicate\Like('some_field', '%'.$value.'%')
)
->OR->->equalTo('my_field', 'bob')
->AND->equalTo('my_field', 'hello');
$select->where($where);

Need listbox with multiple selection - in yii

I need listbox of multiple selection in yii, i have code of form area
but its saving to database as a word "Array" in field, How to handle
this problem?
how to get back while view and update and grid view also
<?php echo $form->dropDownList($model,'clients',
CHtml::listData(client::model()->findAll(array('order'=>'id')), 'id', 'name'),
array('empty'=>'','multiple'=>'multiple','style'=>'width:400px;','size'=>'10'));
?>
Thank you.
For me works this:
'multiple'=>true
Your code must be something like that:
<?php echo $form->dropDownList($model,'clients',
CHtml::listData(client::model()->findAll(array('order'=>'id')), 'id', 'name'),
array('empty'=>'','multiple'=>true ,'style'=>'width:400px;','size'=>'10'));
?>
$htmlOptions = array('size' => '5', 'multiple' => 'true','style'=>'width: 333px');
$model->field_id = array_of_data_to_be_selected
$form->listBox($model,'field_id',$listData, $htmlOptions);
If it is a relation you may want to use this : http://yiiext.github.com/activerecord-relation-behavior/ which takes care of saving array into many to many relation junction table.
Otherwise, like Orlymee said, you need to save each item of the array by looping through it or you can serialize the array or implode it into comma separated values and do the reverse of whatever method you chose to save, while viewing.
keep this code in controller
$arr = implode(",",$model->attributes['hobbies']);
$model->hobbies=$arr;
in controler create,update in the first if condition
in database you can see the values with comma as delimiter
How does this work in CHtml::listBox()
if(!empty($htmlOptions['multiple']))
{
if(substr($name,-2)!=='[]')
$name.='[]';
}
So you can try this
<?php echo $form->dropDownList($model,'clients',
CHtml::listData(client::model()->findAll(array('order'=>'id')), 'id', 'name'),
array(
>>> 'name'=>CHtml::resolveName($model, 'clients').'[]',
'empty'=>'',
'multiple'=>'multiple',
'style'=>'width:400px;',
'size'=>'10',
)
);?>
But it's better to use CHtml::listBox()

Rails 3: convert session[:params] to correct activerecord query

I have two models, Foo and Bar, with same attributes, and I'm 'caching' user "search params" into a session variable, that ends into something like this:
session[:search_params] => {"price"=>"15.0", "air_conditioner"=>"0", "fireplace"=>"0", "number_of_rooms"=>"1", "balcony"=>"0"}
I'd like to search both Foo and Bar for entries that match this query.
For now, I'm doing:
#foo = Foo.new(session[:search_params])
search_attributes = #foo.attributes
search_attributes.delete 'id'
search_attributes.delete 'created_at'
search_attributes.delete 'updated_at'
#results = Foo.where(search_attributes)
#results += Bar.where(search_attributes)
that is REALLY UGLY.
Could anyone tell me a better/the correct way?
Thanks in advance.
EDIT
Also, some params in the session[:search_params] that are "0", are actually 'false' on the database (booleans) (filled by checkboxes), so I have to do some "conversion" into this fields so that the query is done correctly, i.e., getting for example air_conditioner => 'false' instead of air_conditioner => '0'.
I wouldn't follow that path if I were you. I would go with a solution like https://github.com/ernie/meta_search
You can use Dynamic Attribute-Based Finders: api.
So
#results = Foo.find_by_price_and_air_conditioner_and_fireplace_and_number_of_rooms_and_balcony(session[:search_params])
Also, you won't have to delete the extraneous information from session.