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'),
...
);
Related
I am working with a column which stores the data for camera effects usage in a JSON string.
The values inside it look something like this:
{"camera": {"GIFs": ["floss_dance", "kermit"], "filters": ["blur"], "GIF_count": 2, "Filter_count": 1}}
If I want to extract data for GIFs, I use this code:
json_extract_path_text(camera_effects, 'camera', 'GIFs') which will yield the result as ["floss_dance", "kermit"]
If I want to extract particular GIF names, I use json_extract_array_element_text(camera_effects, 'camera', 'GIFs') which will give me floss_dance and kermit in separate rows.
My issue is that we keep adding new effects to our camera, which means the number of elements in the 'camera' array will keep increasing and using the json_extract_path_text(camera_effects, 'camera', 'name_of_effect') is not a dynamic code.
Is there a way to extract a list of all key:value pairs that exist for 'camera' and then use the keys as a column, and values as rows?
Note: I am using Redshift SQL
How can I select data from two columns using Query\Builder with 'LIKE' %something% and 'OR'.
I get no data if there is no match in column title despite the use of the OR, but there must be a match inside the column subtitle.
I'm currently doing this:
$Query->andWhere(
'MyData.title LIKE :searchValue: OR MyData.subtitle LIKE :searchValue:', [
'searchValue' => '%' . $searchValue . '%',
]
);
Well it looks fine. Are you sure there is match? Maybe check what query is produced? You can easily add logs of all queries using 'db:beforeQuery' event.
Maybe you have some other conditions?
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.
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);
In rails 3, I would like to do the following:
SomeModel.where(:some_connection_id => anArrayOfIds).select("some_other_connection_id")
This works, but i get the following from the DB:
[{"some_other_connection_id":254},{"some_other_connection_id":315}]
Now, those id-s are the ones I need, but I am uncapable of making a query that only gives me the ids. I do not want to have to itterate over the resulst, only to get those numbers out. Are there any way for me to do this with something like :
SomeModel.where(:some_connection_id => anArrayOfIds).select("some_other_connection_id").values()
Or something of that nautre?
I have been trying with the ".select_values()" found at Git-hub, but it only returns "some_other_connection_id".
I am not an expert in rails, so this info might be helpful also:
The "SomeModel" is a connecting table, for a many-to-many relation in one of my other models. So, accually what I am trying to do is to, from the array of IDs, get all the entries from the other side of the connection. Basicly I have the source ids, and i want to get the data from the models with all the target ids. If there is a magic way of getting these without me having to do all the sql myself (with some help from active record) it would be really nice!
Thanks :)
Try pluck method
SomeModel.where(:some => condition).pluck("some_field")
it works like
SomeModel.where(:some => condition).select("some_field").map(&:some_field)
SomeModel.where(:some_connection_id => anArrayOfIds).select("some_other_connection_id").map &:some_other_connection_id
This is essentially a shorthand for:
results = SomeModel.where(:some_connection_id => anArrayOfIds).select("some_other_connection_id")
results.map {|row| row.some_other_connection_id}
Look at Array#map for details on map method.
Beware that there is no lazy loading here, as it iterates over the results, but it shouldn't be a problem, unless you want to add more constructs to you query or retrieve some associated objects(which should not be the case as you haven't got the ids for loading the associated objects).