I have to show a select box with the different movie genres grouped by general genres.
To do so, I'm using the CakePHP default method find('list',...).
My database table for movie genres is done like so:
|_id_|_name_________|_general_genre_|
| 1 | Action | Action |
| 2 | Martial arts | Action |
| .. | ............ | ............. |
The problem is that if the group has the same name as the data (e.g.: "Action") the data is not shown in benefit of the group name.
So my select box looks like:
Select a genre
- Action
- Martial arts
- ...
And what I would like is:
Select a genre
- Action
- Action
- Martial arts
- ...
Of course, the reason is, it's not possible to select a group item.
Thanks in advance!
Ok, after some more research I finaly get the answer.
This is an attribute to add to the Html->input() method. This attribute is showParents.
So, if you want to make a select box with groups and have, inside this group, items with the same name that this group, you have to do something like this:
Select the list with the groups:
$options = $this->TableName->find('list', array(
'fields' => array('id', 'name', 'group_name')
));
Show the select input:
$this->Html->input('InputId', array(
'type' => 'select',
'options' => $options,
'showParents' => true
));
Hope this can help ;)
This is expected behavior. Use the showParents option when generating the form element. The API even has an eg. for this case which you failed to check :)
You can override the default fields that a find('list') returns (which are the primary key and the displayField) by your own fields, by setting the $options['fields'] option. These can be more than two fields if you need to. This should come close to what you need:
$this->Genre->find('list', array(
'fields' => array('id', 'name', 'general_genre')
));
This will return an array like:
'Action' =>
1 => 'Action'
2 => 'Martial arts'
You should be able to use that in a dropdown.
Related
Pleas help me out for this minor issues.
I want to remove the original value price from the addition of total optional value price from the cart in OpenCart 3.0.
Reference URL.
When you add option and hit add to cart. After when we open the cart symbol at top right cart, the original price also added.
UPDATED
Thanks for your reply.
I want to display minimum price on home page. So, can I show the product's minimum price from the optional value.
For Example: I have three varieties of products.
Prod 1 ($10)
Prod 2 ($20)
Prod 3 ($30)
I want to display Prod 1 ($10) on my home page. That's it. How to do that?
Appreciate your reply..
In this case you can set original price to 0.
or
You can set main product price = to minimal option price, then set your option prices like:
+0
+10
+20
That's how options in OpenCart works.
UPDATED
Set your product price to $10. Then set your options' prices to:
+0
+10
+20
UPDATE 2
Using previous update, to see full price in option lets make following changes:
open catalog/controller/product/product.php, find
$product_option_value_data[] = array(
'product_option_value_id' => $option_value['product_option_value_id'],
'option_value_id' => $option_value['option_value_id'],
'name' => $option_value['name'],
'image' => $this->model_tool_image->resize($option_value['image'], 50, 50),
'price' => $price,
'price_prefix' => $option_value['price_prefix']
);
This line
'price' => $price,
replace with
'price' => $this->currency->format($option_value['price'] + $product_info['price'], $this->session->data['currency']),
Update your ocmod cache and now you will see (main price + option price) value in options on product page. And set you option from chackbox to radio.
I using REGEXP in wp_query.
I am storing post meta as 20181020 (yyyymmdd)
so basically I want a REGEXP so that I can filter if user enter 10 (mm) in filter box
currently I am using this '^'.$date but this only works if user entered yyyymm format.
I want to search this if use enter value in either yyyy or mm there is no need for dd.
I want to filter based on user input.
User will select month or year,
for example, if user select Dec month then i will get response 10. Then i pass this value in meta_query.
here is the meta query. But in this issue is data stored in post meta is in yyyymmdd format. So I want a regexp where I can query to 4th occurrence.
$args = array(
'post_type' => 'achievements',
'posts_per_page' => -1,
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'achievement_date',
'value' => '^'.$date,
'compare' => 'REGEXP'
)
)
);
$the_query = new WP_Query( $args );
I figured it out.
i just need to use this ^.... expression. This will skip first 4 characters.
I have this below statement in my controller and I want to know how to rewrite it in Rails 4
Entry.find(:all, :conditions => [#conditions.keys.join(" AND "), *#conditions.values],:group => "user_id", :select => "SUM(time_spent) AS total_spent")
The condition has the user_id information in it like "user_id=?".
The statement is trying to find the entry for a particular user id and sum the time spent for the user id and group by user id.
For example you can use it
realations = Entry.where(user_id: [1, 2, 3]).select("SUM(time_spent) as total_spent").group(:user_id)
After that you have relation with which you can work
realations.each do |i|
puts i.total_spent
end
When I use doctrine 2 query builder as below to create a resultset:
$queryBuilder = $this->orm->createQueryBuilder();
$queryBuilder->select("s.id","partial s.{id, name, user}","s.name","concat(u.firstname,' ',u.lastname) fullName")
->add('from', 'Application\Entity\Schools s')
->leftJoin('s.user', 'u', 'WITH', 'u.id = s.user')
->getQuery()->getResult(Query::HYDRATE_ARRAY);
echo "<pre>";
print_r($dd);
die;
It gives following data:
Array
(
[0] => Array
(
[0] => Array
(
[id] => 1
[name] => DAV Public School
)
[id] => 1
[name] => DAV Public School
[fullName] => Manu Sharma
)
[1] => Array
(
[0] => Array
(
[id] => 2
[name] => Delhi Public School
)
[id] => 2
[name] => Delhi Public School
[fullName] => Vipul Sharma
)
)
So when I try to fetch its value using getter method:
$result->getName() doesn't work but I have to use $result[0]->getName().
and error says:
Fatal error: Call to a member function getName() on a non-object
How to choose specific fields inside queryBuilder's select method so that main array values must be accessed using getter methods. Why its not working. Is there anything wrong with my select query.
What is the benefit if specific selected fields can't be accessed using getter method. I believe I'm doing something wrong but it doesn't strike my mind.
Note: While using $result, I removed array_hydrate, that is I'm passing object and hence expecting getter methods to work properly.
The problem you're having is caused by adding additional fields to the SELECT clause. That's causing the result to put the object at index 0 to be able to add additional fields like fullName that you don't have in the entity to the result set.
As far as I understand, all you want is to have a method to get the full name of each user. Instead of solving that problem at the query level, you better just add a method to the User entity:
public function getFullName() {
return sprintf('%s %s', $this->getFirstName(), $this->getLastName());
}
And then just select users the usual way:
SELECT u FROM User u
You can now get the full name of the first user the following way:
$result[0]->getFullName();
You don't need partial objects here. Partial objects have completely different use cases.
Is there any way to fetch objets from the DB and list them by Category? I have a table Artist which have a Genre and each Genre have a Category, so when I'm choosing Genre at new Artist creation I would like to see them separately by each one Category. I recall that in previous versions you could do something like:
$this->widgetSchema['genre_id'] = new sfWidgetFormChoice(array(
'choices' => array('-- Select --',
'-- Category1 --'=>Doctrine_Core::getTable('Genre')->getGenres('Category1')),
'-- Category2 --'=>Doctrine_Core::getTable('Genre')->getGenres('Category2')),
'multiple' => false, 'expanded' => false
));
And the result was a drop down with 'CategoryX' in bold, followed by respectively genres.
After a deep search all I've found is:
->add('group', 'entity', array(
'class' => 'Vendor\MyBundle\Entity\Group',
'label'=>'Group:',
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('g')
->... // whatever you want to do
}
))
I think that is what I need, but have no idea on how to adapt it to what I'm looking for.
Many thanks.