How to Read Fetched Objects in Symfony - php

Hi I'm building a project from symfony and doctrine
I'm trying to access the data in a fetched object. As an example I want to retrieve data in the $products object
$products = $repository->findByPrice($price);
I've tried $prodcuts[0], $prodcuts['pName']
Nothing seems to work. A little advice would be very helpfull

Make sure the namespace and use statements are pointing to correct files, then:
$product = $this->getDoctrine()->getRepository('bundleName:tableEntity')
->findByPrice($price);
Then for instance you can further use
echo $product->getPrice();
You can then handle the object instantiated as per your need..

Related

Programmatically load Entity in symfony

I am trying to load in Entity classes and use within a loop in order to load content in dynamically from files into relating tables.
Is there any way i can load in all Entity files from the following
use AppBundle\Entity\aaPostcode;
use AppBundle\Entity\abPostcode;
use AppBundle\Entity\acPostcode;
use AppBundle\Entity\adPostcode;
in such a way like this?
use AppBundle\Entity\*
Not sure if this is possible in Symfony.
My next issue is using the the prefixedEntity within a loop like so -
new $entityPrefix
When i am setting $entityPrefix to the following format
$entityPrefix = str_replace([".csv"], "", $entityFilename) . "Postcode" . '()';
which returns the string of
"abPostcode()"
can anyone advise as to why calling
new $entityPrefix;
is not working
Thanks in advance for any help!
trying to call
new $entityPrefix();
returns
[Symfony\Component\Debug\Exception\ClassNotFoundException]
Attempted to load class "abPostcode" from the global namespace.
Did you forget a "use" statement for "AppBundle\Entity\abPostcode"?
even when i current am hrd coding the use stament to call
use AppBundle\Entity\abPostcode;
You can't instanciate dynamically your class without ginving the full namespace. Try that :
$namespace = "AppBundle\Entity\\";
$entityName = "YourEntity";
$namespace .= $entityName;
$class = new $namespace();
This is working for me...
I solved this by pulling out the functionality into a helper and running a switch statement based on the input - in this case a {prefix}
I kinda ran into this issue migrating data from one database type to another. IE: Oracle to MySQL. With hundreds of tables the use section would get big.
This is on Symfony 5.2.x BTW.
When I run a query using a standard repository or a repository linked to the table I can call the entity with its full namespace.
$oracle = $em2->getRepository(\App\Entity\Oracle\Appoints::class)->Appoints();
So I did not have to load "use App\Entity\Oracle\Appoints
Then I can create a new object in the same way:
$mysqlObject = new \App\Entity\OracleAppPoints();

FOSElasticaBundle: Is it possible to change "query_builder_method" in controller?

According to FOSElasticaBundle documentation it is possible to configure application to use custom query builder method like this:
user:
persistence:
elastica_to_model_transformer:
query_builder_method: createSearchQueryBuilder
But is it possible to choose QB method live, e.g. in controller action?
I'd like to be able to control what's being fetched from DB while transforming Elastica results to Doctrine entities. E.g. sometimes I'll want to do eager fetch on some relations, but can't do that by default.
Since FOSElasticaBundle documentation is not very precise, I went through its code and found it impossible to control what query builder is used on controller level.
It is possible to change whole elastica_to_model_transformer to a custom service, but still it's statically defined in configuration. Maybe with some dirty solution it would be possible going this way, but I don't think it's worth it.
I decided to just not using this feature of FOSElasticaBundle. The main problem I had was that when you use fos_elastica.index instead of fos_elastica.finder or elastica repository (in order to get plain not transformed results Elastica\Resultset), there's no findPaginated method with returns Pagerfanta paginator object, which is very helpful in my case.
Fortunately although it's not mentioned in documentation it's possible to create the Pagerfanta this way too, but a little bit more manually.
Here's a code snippet:
//generate ElaticaQuery somehow.
$browseQuery = $browseData->getBrowseQuery();
$search = $this->container->get('fos_elastica.index.indexName.typName');
//create pagerfanta's adapter manually
$adapter = new \Pagerfanta\Adapter\ElasticaAdapterElasticaAdapter($search, $browseQuery);
// now you can create the paginator too.
$pager = new Pagerfanta($adapter);
//do some paging work on it...
$pager->setMaxPerPage($browseData->getPerPage());
try {
$pager->setCurrentPage($browseData->getPage());
} catch(OutOfRangeCurrentPageException $e) {
$pager->setCurrentPage(1);
}
//and get current page results.
/** #var Result[] $elasticaResults */
$elasticaResults = $pager->getCurrentPageResults();
// we have to grab ids manyally, but it's done the same way inside FOSElasticaBundle with previous approach
$ids = array();
foreach($elasticaResults as $elasticaResult) {
$ids[] = $elasticaResult->getId();
}
//use regular Doctrine's repository to fetch Entities any way you want.
$entities = $this->getDoctrine()->getRepository(MyEntity::class)->findByIdentifiers($ids);
This actually has a few advantages. In general it gives you back control over your data and doesn't tie ElasticSearch with Doctrine. Therefore you can resign on fetching data from Doctrine if you have all needed data in ElasticSearch (if they are read only data of course). This lets you optimize your application performance but reducing amount of SQL queries.
The code above may be wrapped with some kind of service in order to prevent making mess in controllers.

Export objects from class issue

I have a product export and I try to export all objects using my custom plugins. I'm using below method to load all objects
$list = new Pimcore_Model_Object_Bodproduct();
$list->setOrder ( "ASC" );
$list->setOrderKey ( "o_id" );
// Load all filtered objects - products
$list->load ();
Then i'm using foreach loop to access the objects.
It's working fine. But the problem is it's taking lot of time to export. Is there any other solutions for this .. I mean instead of load() function, is there any other method is available?
You can use $list->loadIdList() to fetch only object IDs - which should be faster. Then you can call Object::getById($id) within loop to provide progress bar (assuming your script is launched in console).
In fact this is what load() is doing internally, please see Object\Listing\Resource class
Another option is to split load to many "pages" by calling $list->getItems($offset, $itemCountPerPage).
In Pimcore object listing, you can traverse each object in list without calling a load method. In your case, just remove "$list->load ();" and see it will work with less time.

Is there an easier way to iterate through a list of objects properties in Laravel

I have a Laravel app where I am using a bit of code which feels really unintuitive.
In the code I return a list of objects ($occupied) which all have the the column 'property'. I then go on to create an array of the list of objects 'property's ($occupiedproperty) just to use it in a whereNotIn call.
if ($occupied = Residency::currentResidents()){
// Here is the pointless part //////
$occupiedproperty = array();
foreach ($occupied as $occ) {
array_push($occupiedproperty, $occ->property);
}
///////////////////////////////////
return Property::whereNotIn('id', $occupiedproperty)->get();
}
This code works fine to do the job but creating a new array when I already have a list of objects seems lazy. I tried looking at eloquent's documentation but I couldn't figure this out.
I need to be able to access the 'property' column of $occupied so I can run something like whereNotIn('id', $occupied->property)
Thank you
Can't test it right now, but this should work (it should work even without casting to array the $occupied collection):
$occupiedProperties = array_pluck((array)$occupied, 'property');
It uses the array_pluck() helper method: http://laravel.com/docs/4.2/helpers#arrays

Where to instantiate objects for a collection if the DataMapper is only for transfer/exchange of data?

Since the DataMapper is supposed to be for the exchange/transfer of data between objects and the relational database I would get a user like this
$user = $this->entityFactory->build('User');
$userMapper = $this->mapperFactory->build('User');
$user->setId(43);
$userMapper->fetch($user);
That works fine because I can create the User object outside of the mapper and pass it in but what do I do when I am getting a collection/list of objects?
Creating the empty objects outside of the mapper first just does not seem correct and would surely cause some problems so what is the best way to do it?
Thanks.
I don't know if this question is still in your mind, however let me give you an answer to this. In principle the first step is the same
$userCollection = $this->entityFactory->build('UserCollection');
$userCollectionMapper = $this->mapperFactory->build('UserCollection');
$user = $this->entityFactory->build('User');
$user->setId(43);
$userCollection->add($user);
$userCollectionMapper->fetch($userCollection);
So the userObject would function here as an searchObject for the collectionMapper (like teresko proposed in an older thread). Your CollectionMapper would retrieve the data from the database and i prefer to use something like
//returns new User object
$newUser = $userCollection->newUser();
//populate user object with DB entry
$userCollectionMapper->populate($newUser,$DBresponse);
//add to collection
$userCollection->add($newUser);
Of course there would be a loop before that looping through the found lines in the database and you would have to clear the list of user objects before adding the results.
So this is the way i would deal with the problem. Hope it helps.

Categories