I want count the number of entities with Cakephp 3.0, so, I read the book, and i found the query
$number = $this->Models->find()->where(['variables' => 1])->count();
But now, how view the number of entities in my view ?
I have :
$this->set(compact('number'));
Thanks !
Just by using this in your view :<?=$number?>
Related
I have a custom symfony bundle used in Akeneo 1.6 (Upgraded from 1.3 some time ago), which I need to port to Akeneo 2.2.
What I need is get all product data by product ID in a controller action.
This was done by $repository->getFullProduct($productId). This Method was removed in the current Akeneo version.
While researching I found that the "standard format" seemed helpful as it contains all product data as array.
How can I recieve this data? It doesn't have to be a clean solution, Quick&Dirty (Like the whole Bundle ;) ) is just fine. It's only for internal use.
I tried some stuff like $productStandard = $this->container->get('pim_api.normalizer.product')->normalize($product); whith different services, but based on the useless error messages I recieved I think it just doesn't make sense.
When searching products in Akeneo PIM, you should use the Product Query Builder. You can read more about it on the official documentation on querying products. A quite similar question has been asked, you can see my answer here: Query products with Doctrine ind Akeneo.
To get the standard format of a product, you can normalize your Product instance with the normalizer.
So this would look like this:
<?php
// Get a new instance of the PQB
$pqbFactory = $this->getContainer()->get('pim_catalog.query.product_query_builder_factory');
$pqb = $pqbFactory->create([
'default_locale' => 'en_US',
'default_scope' => 'ecommerce'
]);
// Now you can search for products with your ids
$pqb->addFilter(
'id',
'IN',
['234', '22', '90']
);
// Retrieve your products
$productsCursor = $pqb->execute();
$normalizedProducts = [];
foreach ($productsCursor as $product) {
// normalize them to the standard format
$normalizedProducts[] = $this->getContainer()->get('pim_standard_format_serializer')->normalize($product, 'standard');
}
I just made an entity 'car'
I have added 10 of these entities in the drupal cms.
Now i want all ID's of the entitys 'car' in my controller
This is what i tried so far that did not work:
$query = \Drupal::entityQuery('car');
$query->condition('status', 1);
$ids = $query->execute();
$entity_query = \Drupal::service('entity.query')->get('car');
$uids = $entity_query->execute();
Both give me much more then just the 'car' entity.
they deliver other content types as well
Can someone help me how i can get all published 'car' nodes (id's)
so i can get all info for them with the load() command?
tnx
Problem solved:
Create an instance of the entity.
Then use that instance to build the entityForm
$entity = $this->entityTypeManager()->getStorage('car')->create(array());
$form = \Drupal::service('entity.form_builder')->getForm($entity,'default');
(I did create the entity using the drupal console and the default form you can set in the annotation section form in the entity file)
I have a Drupal 8 content entity with a relation to a taxonomy term which allows multiple values.
I want to query the entity and get content that has only the terms I use in the query.
The problem is I could not find a way to query my entity with multiple taxonomy terms and get the content that are associated with them.
My content entity (node bundle) is called "cocktails" and has among others an entity reference field called "field_ingredients" that has a relation to the taxonomy Vocabulary "Ingredients".
I want to get the entity that has for example 2 ingredients with the taxonomy ids: 40 AND 35. I tried the following code without success:
$query = \Drupal::entityQuery('node');
$query->condition('type', 'cocktails');
$query->condition('field_ingredients.entity.tid', 40);
$query->condition('field_ingredients.entity.tid', 35);
$node_ids = $query->execute();
$node_ids are 0
And also this:
$query = \Drupal::entityQuery('node');
$query->condition('type', 'cocktails');
$query->condition('field_ingredients.entity.tid', array(40, 35), 'IN');
$node_ids = $query->execute();
$node_ids are 3 which returns the node ids that have one of the two taxonomy ids (OR logic),
The right answer should be one node id, which is the node that is related to both taxonomy ids, the cocktail with both ingredients
Finally the solution was posted in the actual Drupal API Documentation as a comment
$query->condition(
$query
->andConditionGroup()
->condition('field_ingredients', [40, 35,])
)
);
look at the following link for more details:
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Entity!Query!QueryInterface.php/function/QueryInterface%3A%3AandConditionGroup/8.2.x
on the first comment and first example ("WORKING code")
I currently use the Criteria to filter a collection of objects. But when I want to achieve with 2 orderBy fields, only the first is considered. I do not understand.
$events = new Collections\ArrayCollection($results);
$dateFrom = new \DateTime($date);
$dateTo = new \DateTime(date('Y-m-d H:i:s', strtotime($date . ' + 1 day')));
$criteria = Criteria::create()
->where(Criteria::expr()->eq('activity', $activity));
$criteria->orderBy(array(
"time" => "ASC",
"title" => "ASC"
));
How can I make it work with two orderby fields and not only the first ?
Thank you in advance for any answers !
Your code is correct, assuming that $criteria is then applied correctly to the ArrayCollection.
Judging by the names of your fields you're trying to order by, it's possible that title doesn't affect the order because time doesn't repeat among the elements in the collection.
If this isn't the case, please provide more information on the results you're getting and I will update my answer.
Update (in response to additional data provided that has since been deleted):
You are sorting the collection correctly. The problem is that you're then feeding the ArrayCollection into the Paginator, which apparently cannot sort by more than one field.
There's an open issue in Knp's tracker about this: https://github.com/KnpLabs/KnpPaginatorBundle/issues/109.
Making a search with Apachesolr, i want to add a couple of filters in hook_apachesolr_prepare_query(&$query). This works fine, except I want the filters to widen the search ('OR'), rather than narrow it ('AND').
For example, if I have 4 nodes of type:A and 3 of type:B that match a search, to filter by type:A and type:B should return 7 nodes (of type:A AND nodes of type:B), rather than 0 those of type:A which are also of type:B.
I saw a suggestion to do this using the model of nodeaccess
foreach ($filters as $filter) {
$subquery = apachesolr_drupal_query();
if (!empty($subquery)) {
$subquery->add_filter('type', $filter);
$query->add_subquery($subquery);
}
}
but this doesn't seem to work. (It doesn't return any results).
I then tried (as I have a limited number of node types) excluding the types that I don't want:
$excludes = array('A', 'B', 'C');
$excludes = array_diff($excludes, $filters);
$exclude = implode('&', $excludes);
$query->add_filter('type', $exclude, TRUE);
This method of stitching them together doesn't work (the '&' gets escaped) but neither does adding them as subqueries, similar to the manner above.
Any suggestions on how to do this?
With Drupal7 and the last apacheSolr API, you can do OR filters by doing this :
function my_module_apachesolr_query_alter($query) {
// first, create a subQuery filter to store others
// and specify there is a "OR" condition
$filter = new SolrFilterSubQuery('OR');
// next, add all filters on bundle you want, each as
// a new subQuery filter, always with "OR" condition
// and add it to the primary filter
$a = new SolrFilterSubQuery('OR');
$a->addFilter('bundle', 'A');
$filter->addFilterSubQuery( $a );
$b = new SolrFilterSubQuery('OR');
$b->addFilter('bundle', 'B');
$filter->addFilterSubQuery( $b );
$c = new SolrFilterSubQuery('OR');
$c->addFilter('bundle', 'C');
$filter->addFilterSubQuery( $c );
// finally, add the primary subQuery filter as
// subquery of the current query
$query->addFilterSubQuery( $filter );
}
And your query search about type A OR type B OR type C (all results in each types). You can combine OR / AND by changing the parameter of the SolrFilterSubQuery instanciation.
Special thanks to this page and it's author : http://fr.ench.info/blog/2012/04/03/Add-Filters-ApacheSOLR.html
I havenť played with SOLR much but I am quiete familiar with Drupal and Zend Lucene (or Drupal Lucene API).
I would suggest that you try to filter your results based on content type (because each node has its content type stored in the object).
The second idea is to change basic operator. I am not sure how it is done in SOLR but i Zend Lucene
Zend_Search_Lucene_Search_QueryParser::setDefaultOperator($operator) and Zend_Search_Lucene_Search_QueryParser::getDefaultOperator() methods, respectively.
Docs can be found in Zend Lucene Docs. Or for SOLR Solr Docs.
I hope a got your problem right.
Hope it helps :-)