I wantend to create a complex grouping using doctrine query and I used Snippets:
Snippet Doctrine custom grouping
$q = myDQ::create()
->from('Some s');
It gives me a mistake:
Strict Standards: Declaration of MyDq::create() shuld be compatible with that of Doctrine_Query::create() in URL-ROOT/lib/MyDq.class.php
Doctrine_Query:create is declared this way:
public static function create($conn = null, $class = null)
you did:
public static function create($conn = null)
which doesn't work because of the different arguments.
For information on overriding static methods you may also find this page interesting: php.net/manual/language.oop5.static.php
Related
I have spent the last few hours trying to find an answer however not gaining at all. I feel that it will end up being something simple that I did not have knowledge of. This section of code is of course incorrect but I wanted to show the idea of what I was trying to do here. I currently have a base repository class that has the findAllQueryBuilder method within it that will allow for reduced code. Right now I have about 10 repository classes that all need to use findAllQueryBuilder. This function is within the basecontroller I have built. The main issue is that this controllor does not see findAllQueryBuilder because as you can see this takes in the parameters to determine which data location is needed. I've done this once already in another location in my code with an interface however that was with a class I will put that example here also.
public function listAction(Request $request, RepositoryTypeInterface $repositoryName, $table, $sql, $route)
{
$filter = $request->query->get('filter');
$repository = "Bundle:$repositoryName";
$qb = $this->getDoctrine()
->getRepository($repository, 'tenant')
->findAllQueryBuilder($filter, $table, $sql);
As you can see in the following I set the type as the interface to make sure it knew that it was going to be that then.
public function newAction(Request $request, EntityTypeInterface $controllerType, formType, $repository, $routeName)
{
And then instantiated it within the childclass controllor
public function newAction(Request $request, EntityTypeInterface $controllerType = null, $formType = ContactType::class, $repository = 'Contact', $routeName = 'api_contacts_show')
{
$controllerType = new Contact();
return parent::newAction($request, $controllerType, $formType, $repository, $routeName);
}
So The first example above has me attempting the same thing however I am not sure how to apply this to this situation. as "Bundle:$repository" is a string and the parameter used is a string and it's not an entity so creating an instance doesn't make sense when all I need is its functionality. I just need some way to have access to this functionality. any ideas would work but I'm feeling like I'm missing something simple.
Here is the exact error I get,
Method 'findAllQueryBuilder' not found in \Doctrine\Common\Persistence\ObjectRepository
Referenced method is not found in subject class.
I'm sure there is a way to apply this concept to fix the lack of 'seeing' the function however as of right now I'm not completely sure.
Thank you anyone in advance.
EDIT:
I'm using a basecontrollor that is setting this up for the other controllers ie:cantactscontrollor class which uses the ContactEntity class mapped to the contactrepository class which is a child of a baserepository class which is where findAllQueryBuilder is located, I'm not sure how to map that.
public function listAction(Request $request, $repositoryName, $table, $sql, $route)
{
$filter = $request->query->get('filter');
$repository = "TenantBundle:$repositoryName";
/** #var RepositoryTypeInterface $qb */
$qb = $this->getDoctrine()
->getRepository($repository, 'tenant');
$queryResults = $qb->findAllQueryBuilder($filter, $table, $sql);
Sorry for the late response on this but here is the answer I was able to find. As I thought it was a simple thing but essentially the Docblock labeled $qb as the said interface above making sure it knew that any class that implements it will be accepted. Now knowing what type it will be, the error message is no longer there. Notice also how I split it up.
I'm developing a web app, and I might have logic such as "When Field A = value and Field B = other value, then this should be shown in the frontend." I might have different lists, which further elaborate on this. I might even have translated entities which have the translation in a different entity (example: Entity and EntityTranslation) and want to join them only on a specific locale.
How could I reuse all this logic, to avoid repeating the same QueryBuilder::andWhere() and QueryBuilder::join() (even QueryBuilder::select()) calls all over the place?
I've found http://www.whitewashing.de/2013/03/04/doctrine_repositories.html which talks on this, but I'm curious about solutions which involve also JOINs and SELECT.
EDIT:
Bad example of what I'd want:
$queryBuilder
->andWhere(FRONTEND_LOGIC)
->joinWithTranslationTable();
So I'd want to be able to compose "complex" queries from my, simpler, but Buisiness driven, parts.
You can use prepared statements in Doctrine's DBAL layer.
$dbal = $this->getDoctrine()->getConnection('default');
$stmt = 'SELECT name, birth from user where id = :id';
$user1 = $dbal->executeQuery( $stmt , array( 'id' => 1 ) )->fetchAll() ;
$user2 = $dbal->executeQuery( $stmt , array( 'id' => 2 ) )->fetchAll() ;
Refer to the documentation for other methods suitable for your task:
http://docs.doctrine-project.org/projects/doctrine-dbal/en/2.0.x/reference/data-retrieval-and-manipulation.html#using-prepared-statements
Doctrine does not allow you to specify the query builder class so you cannot directly extend it. You can however decorate it.
class MyQueryBuilder
{
public function __construct($doctrineQueryBuilder) { this->doctrineQueryBuilder = $doctrineQueryBuilder; }
// Your custom functions
public function addWhereFrontEndLogic ...
public function joinWithTranslationTable ...
// Also need to add the the regular methods that you use
public function addSelect($value) { return $this->doctrineQueryBuilder($value); }
// You would then
$qb = new MyQueryBuilder($this->createQueryBuilder());
Bit of a pain to have to add all the standard functions but once you have done it then you can add as much custom business logic as you want.
There are some other possible approaches. You could add the custom methods to a base repository class and then just call the methods with the doctrine $qb as an argument.
I am very new to Solr and I am probably missing something simple however, having followed Xavier Briand's presentation I have set up Symfony2, Solarium and Nelmio\SolariumBundle.
"nelmio/solarium-bundle": "2.0.*#dev",
"solarium/solarium": "3.0.*"
Having implemented a toSolrDocument method for my doctrine php object.
public function toSolrDocument(\Solarium_Document_ReadWrite $doc)
{
$doc->id = $this->getId();
$doc->description = $this->getTitle();
$doc->path = "path";
return $doc;
}
I am faced with the following error.
Catchable Fatal Error: Argument 1 passed to ::toSolrDocument() must be an instance of Solarium_Document_ReadWrite, instance of Solarium\QueryType\Update\Query\Document given, called in Controller.php
The controller calling this toSolrDocument method has the following function
public function indexItems(){
$client = $this->get('solarium.client');
// get an update query instance
$update = $client->createUpdate();
// create documents
$documents = array();
$em = $this->getDoctrine()->getManager();
$repo = $em->getRepository('<Bundle>:<Object>');
$items = $repo->findAll();
foreach ($items as $item) {
$documents[] = $item->toSolrDocument($update->createDocument());
}
// add the documents and a commit command to the update query
$update->addDocuments($documents);
$update->addCommit();
// this executes the query and returns the result
$result = $client->update($update);
}
Most of this method again comes directly from Xavier's presentation and it is clear to see that the method $update->createDocument() does not return the correct type. In fact it returns an instance of the my php object. Does anyone know where I am going wrong here? Another thing that might be of help is that even when I try to pass a Solarium Document directly I get an exception.
foreach ($items as $item) {
$rw_item = new \Solarium_Document_ReadWrite();
$documents[] = $item->toSolrDocument($rw_item);
}
The exception is
FatalErrorException: Error: Class 'Solarium_Document_ReadWrite' not found in
I can't seem to find this class in any of the bundles and I am wondering if my setup might be causing the issues. Any help would be very much appreciated.
One additional point to note is that when I am running the solr jar I see the query requests come in from my symfony2 page it is only this indexing action that I can not work out so the config may be alright and I am miss understanding the use of the bundle.
You just need to use the correct class for the argument.
use Solarium\QueryType\Select\Result\AbstractDocument;
...
public function toSolrDocument(AbstractDocument $doc)
{
You could also not type hint it:
public function toSolrDocument($doc)
{
In my extbase/fluid project,in addition to standard actions such as create,delete,list etc, I want to create a duplicate of a model class object which are stored in a repository. Using findall(), all objects are displayed in a list and corresponding actions such as delete,edit are displayed next to each. For duplicating an object, I have created a duplicate action in the corresponding controller and here is the code:
public function dupcliateAction(Tx_CcCompanylogin_Domain_Model_MyObject $testObject)
{
$this->myObjectRepository->add($testObject);
$this->redirect('list');//Lists the objects again from the repository
}
Seems straitforward enough but no new object is added to the repository and I am not getting an error.I have checked the documentation and there is no explicit method available for duplicating.
For those who may concern:
You don't need to call the reflection-api at this point.
You only need to implement a method called e.g. resetUid() in your model like this:
public function resetUid() {
$this->uid = NULL;
$this->_setClone(FALSE);
}
then you can use the magic clone method to clone the object in your controller. after that you have to call the new resetUid() method and then you are able to persist the new object with the old properties.
Note : When an object is cloned, PHP 5 will perform a shallow copy of all of the object's properties. Any properties that are references to other variables, will remain references.
Alternative you can use reflection to create a (deep) copy of your object.
$productClone = $this->objectManager->create('Tx_Theext_Domain_Model_Product');
// $product = source object
$productProperties = Tx_Extbase_Reflection_ObjectAccess::getAccessibleProperties($product);
foreach ($productProperties as $propertyName => $propertyValue) {
Tx_Extbase_Reflection_ObjectAccess::setProperty($productClone, $propertyName, $propertyValue);
}
// $productAdditions = ObjectStorage property
$productAdditions = $product->getProductAddition();
$newStorage = $this->objectManager->get('Tx_Extbase_Persistence_ObjectStorage');
foreach ($productAdditions as $productAddition) {
$productAdditionClone = $this->objectManager->create('Tx_Theext_Domain_Model_ProductAddition');
$productAdditionProperties = Tx_Extbase_Reflection_ObjectAccess::getAccessibleProperties($productAddition);
foreach ($productAdditionProperties as $propertyName => $propertyValue) {
Tx_Extbase_Reflection_ObjectAccess::setProperty($productAdditionClone, $propertyName, $propertyValue);
}
$newStorage->attach($productAdditionClone);
}
$productClone->setProductAddition($newStorage);
// This have to be repeat for every ObjectStorage property, or write a service.
For me, the Solution "Clone $Object and resetUid() in the Modell" does not work .. maybe this solution had worked in older TYPO3 Versions but in 7.6. LTS it throws an exception
#1222871239: The uid "61" has been modified, that is simply too much.
so maybe someone can find my solution helpful as it is much less code than the other reflection Solution: (and you do not have to think about to set all single properties .. )
Given: an Object called $registrant with all Data.
Wanted Result: a copy of that Object with same Data, but new Uid ..
/** #var \JVE\JvEvents\Domain\Model\Registrant $newregistrant */
$newregistrant = $this->objectManager->get( "JVE\\JvEvents\\Domain\\Model\\Registrant") ;
$properties = $registrant->_getProperties() ;
unset($properties['uid']) ;
foreach ($properties as $key => $value ) {
$newregistrant->_setProperty( $key , $value ) ;
}
I think the add command ignores objects that already exist.
You could try to clone the object and then add the clone to the repository $copy_of_object = clone $object;. Or maybe create a new Object with all the same properties.
You know that in Symfony2 a new entity can be defined as in the following example:
use Acme\StoreBundle\Entity\Product;
public function defaultController() {
$product = new Product();
$product->setName('Pippo');
$product->setPrice(19.99);
....
// Use Doctrine EntityManager to store the Product object
}
Suppose that you know that the Product class has the following namespace: "AcmeHomeBundle:Product". It would by nice to create the $product object by using the namespace (e.g. by using the EntityManager or something similar).
public function defaultController() {
$item = createObjectFromNamespace("AcmeHomeBundle:Product");
$item->setName('Pippo');
$item->setPrice(19.99);
....
// Use Doctrine EntityManager to store the Item object
}
Do you know if this is possible?
Suppose that you have a string that provides the entity type
You should do this...
$entityInfo = $this->em->getClassMetadata("entityNameSpace:entityName");
$entityMember = $entityInfo->newInstance();
If you wanna use a setter method by string:
$entitySetMethod = "set".\ucfirst("entityDataMemberName");
\call_user_func(array($entityMember, $entitySetMethod), $parameter);
If you really want to, you can do this:
$product = new Acme\JournalBundle\Entity\Product();
$article = new Acme\JournalBundle\Entity\Article();
But you'd have to type it out every time you wanted to create a new entity in that namespace. If you simply used a use statement at the top of you class:
use Acme\JournalBundle\Entity\Product,
Acme\JournalBundle\Entity\Article;
You could then create new articles and products with a simple:
$product = new Product();
$article = new Article();
They do the same thing.
Acme\StoreBundle\Entity\Product IS the namespace of your entity. AcmeStoreBundle:Product is just an alias for the namespace to be used in DQL as a shorter alternative to the real namespace.
Why would you want to create objects with aliased namespace? I suppose you could create some kind of a factory using alias to map it to a real namespace, create an object and return it. But what's the point?
Entity aliases are defined via Configuration: http://www.doctrine-project.org/api/orm/2.2/source-class-Doctrine.ORM.Configuration.html#153
you can not only set them but also retrieve, so if you really need this functionality you should be able to do this with Configuration instance.
It's hard to find anything about entity aliases in Doctrine docs. Symfony docs explain the purpose of it a little:
alias - Doctrine offers a way to alias entity namespaces to simpler, shorter names to be used in DQL queries or for Repository access. When using a bundle the alias defaults to the bundle name.