1

I have a data transformer where I am trying to check for duplicates in my tagging system.

Code:

use Doctrine\Common\Persistence\ObjectManager; use Symfony\Component\Form\DataTransformerInterface; use Doctrine\Common\Collections\ArrayCollection; class TagsToCollectionTransformer implements DataTransformerInterface { private $manager; public function __construct(ObjectManager $manager) { $this->manager = $manager; } public function transform($tags) { return $tags; } public function reverseTransform($tags) { $tagCollection = new ArrayCollection(); $tagsRepository = $this->manager->getRepository('MyThorluxBundle:Tag'); foreach ($tags as $tag) { $tagInRepo = $tagsRepository->findBy(['name' => $tag->getName(); if ($tagInRepo !== null) { // Add tag from repository if found $tagCollection->add($tagInRepo); } else { // Otherwise add new tag $tagCollection->add($tag); } } return $tagCollection; } } 

I have changed the findOneByname method to the FindBy shown above however I cannot access any of the methods of $tag even though I get the repository. I should be able to call getname() on $tag yet it cannot find the method?

Thanks.

7
  • You can use findBy(['name' => $tag->getName()]) Commented May 15, 2017 at 13:16
  • 1
    Thanks for your reply. yes that makes sense. If you look at my edit, I can't access any methods on $tag. Why could this be? Commented May 15, 2017 at 13:18
  • I see you changed findByName to findOneByName. Which is good though by not updating your actual code you have made your question difficult to follow. Now it seems like you have problems with $tag? Completely different issue. I would actually suggest clearing up your previous question before forging ahead with new stuff. Commented May 15, 2017 at 13:24
  • And just out of curiosity, why do you think the MainMedia repository will gave you direct access to tags? Commented May 15, 2017 at 13:25
  • Question updated. Commented May 15, 2017 at 13:29

1 Answer 1

2

Methods with findBySTH are created dynamically by using php magic method __call

You won't find the source of this method but you can take a look at __call method if you want.

If you call findByName($name) what it does it's actually using findBy method with $criteria['name'] => 'value of name'

You can read more on http://symfony.com/doc/current/doctrine.html

You can also actually write this method in MyThorluxBundle:MainMedia repository so _call won't be invoked or simply call findBy(['name' => 'your tag']);

Sign up to request clarification or add additional context in comments.

2 Comments

Hi there, that makes sense. I cant find any of the tag methods? Why could this be? The tag variable should give me access to the entities methods correct?
It may be IDE issue, if you want to be sure what is the object then you can check with instance of and then you can mark it with @var annotation