I want to show in my AssociationField in Easyadmin a item with ID = 1. I have:
AssociationField::new('MYOBJECT')->setColumns('col-sm-3 col-xxl-3')
->setQueryBuilder(
fn (QueryBuilder $queryBuilder) => $queryBuilder->getEntityManager()->getRepository(OBJECTS::class)->find(1)),
It doesn't work. I've also found this:
https://github.com/EasyCorp/EasyAdminBundle/issues/5081
What's the way to filter the entries in Association field?
Related
it's possible to create a QueryBuilder in Symfony with fields that not appear in the Entity Class.
Something like this:
MyEntity Class:
id, name, color
DB Table: id, name, color, number
And then, I'm triying to do this:
$filterBuilder = $this->get('doctrine.orm.entity_manager')
->getRepository('XXXXBundle:MiEntity')
->createQueryBuilder('o')
->select('o.number')
->..........
;
But I get this error:
Error: Class XXXXX has no field or association named number
In your case, you should still add an unmapped "number" proporty to your class, but if you insist, you can use a native query to fetch whatever you want from the DB.
Here is an example that selects all users with an age > 20 :
$query = $em->createQuery('SELECT u FROM MyProject\Model\User u
WHERE u.age > 20');
$users = $query->getResult();
As seen on Doctrine Query Language documentation page
I would like to give formBuilder User Entity as hidden value.
$form->add('user','hidden',array("data" => $user))
$user is User Entity.
However it shows this error.
Expected argument of type "Acme\UserBundle\Entity\User", "string" given
If I use 'null' instead of 'hidden'
$form->add('user',null,array("data" => $user))
it doesn't show the error and shows the select box of user Entity.
However I would like to use hidden.
How can I make it??
You did't specify the field type correctly - this is the correct way:
...
$formBuilder->add('user', HiddenType::class);
...
...
$form = $formBuilder->getForm();
$form->get('user')->setData($user->getId());
But you can't assign entity to the hidden field, so you can assign user's id for user identification.
Another option is to make data transformer and define own EntityHiddenType - more on this here: symfony : can't we have a hidden entity field?
I'm trying to find the right way to handel the next tabel setup.
Products is my main table and contains an id.
UsersCarts has an field product_id and with belongsTo it also loads the products.
So far no problem. I also have an table ProductsNettoDeals where I store possible netto prices. This table has it's own id an an product_id for the hasOne relation when loading products in the productcontroller.
When I load the UsersChart it loads it belongsTo products, but I also want the association with the table ProductsNettoDeals, but this table is connected with products through the id (products.id = products_netto_deals.product_id). I cant find the right syntax or association for the querybuilder. What I have so far:
// src/Model/Table/UsersCartsTable.php
namespace App\Model\Table;
use Cake\ORM\Table;
use Cake\ORM\Query;
use Cake\Validation\Validator;
class UsersCartsTable extends Table
{
public function initialize(array $config)
{
$this->addBehavior('Timestamp');
$this->belongsTo('Products');
$this->belongsTo('ProductsNettoDeals');
}
public function validationDefault(Validator $validator)
{
return $validator
->notEmpty('user_id', 'A user id is required')
->notEmpty('product_id', 'A product id is required');
}
public function findCart(Query $query, array $options)
{
$query
->where([
'user_id' => $options['user_id'],
//'active' => '1'
])
->contain(['Products','ProductsNettoDeals'])
//->select(['product_id','created','products.name','products.price','products.delivery_time']);
;
return $query;
}
}
In this setup I always have the problem that the matching of the id's are wrong. With the products it's good. The users_carts.product_id links with the products.id, but the products_netto_deals.product_id should link with product.id and not with the users_carts.id. I tryed foreign key and bind, but both are an part of an solution.
Beside this problem, when connecting the association ProductsNettoDeals I get an error when activating the commented active => '1'. This field indicates if an record is active and should be loaded. The same field exist in the products_netto_deals tabel to indicate if an netto price is active.
Error: SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'active' in where clause is ambiguous
Can anyone point me in the right direction for the correct way of the association and the error warning. Thanks in forward.
CakePHP uses joins for fetching associations, in order to speed ud the information retrieval process. This means that when having column names repeated across different table, you will need to prefix the column names in the query clause"
Instead of:
$query->where(['active' => true]);
Do:
$query->where(['UsersCarts.active' => true])
I'm wondering if it's possible to querying doctrine entities by a property from a relation.
Here is an example :
Entity A fields :
-> title
-> content
-> description
-> date
Entity B fields :
-> title
-> link ( entity b )
-> date
Is it possible to querying Entity B by link->title property such as the following :
$this->getDoctrine()->getManager()->getRepository("acmeAppBundle:EntityB")->findBy(array( "title" => "test", "link.title" => "example" ) );
Currently I'm achieving this with a custom function from Entity B repository, but may be I'm missing something.
You can't use findBy like this. FindBy is only there to fetch very basic things. Generally it's considered as a best practice to use repository calls, because for example, if you here fetch all objects where title is test and then you get from A all B entity, then entity B will be fetched separately, while in a repository call you can use join, so only one query will be sent to your DB.
I have an Entity called 'User', which has a OnetoMany relation in a field called 'books'.
I also have an Entity called 'Book' which has many attributes, like, 'title' or 'date'.
There ir another entity called 'Date', which attributes are: 'day', 'month' and 'year'.
I want to filter the books that the user has which are from year 2009.
I am tring to do it like this, using Criteria, but I recieve an error because field date.year does not exist:
$books = $user->getBooks();
$criteria = Criteria::create()
->where(Criteria::expr()->eq("date.year", "2009"));
$books_2009 = $books->matching($criteria);
Any idea of how can I solve it?
$criteria = Criteria::create()
->where(Criteria::expr()->eq("year", "2009"));
This should work. Please let me know.