use directory is not working in laravel-5.5 - laravel-5.5

the problem is quite simple the
"(1/1) FatalThrowableError
Class 'app\user' not found"
however i include use app/user in my controller file but still it happen here is my code...
class userController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$data = user::all();
return view('user.index',compact('data'));
}

type use App\User; take note of the capital letter A and capital letter U.

Related

Doctrine2 - How to define table prefix for orm mapping [duplicate]

Like in question topic, how can I setup default table prefix in symfony2?
The best if it can be set by default for all entities, but with option to override for individual ones.
Having just figured this out myself, I'd like to shed some light on exactly how to accomplish this.
Symfony 2 & Doctrine 2.1
Note: I use YML for config, so that's what I'll be showing.
Instructions
Open up your bundle's Resources/config/services.yml
Define a table prefix parameter:
Be sure to change mybundle and myprefix_
parameters:
mybundle.db.table_prefix: myprefix_
Add a new service:
services:
mybundle.tblprefix_subscriber:
class: MyBundle\Subscriber\TablePrefixSubscriber
arguments: [%mybundle.db.table_prefix%]
tags:
- { name: doctrine.event_subscriber }
Create MyBundle\Subscriber\TablePrefixSubscriber.php
<?php
namespace MyBundle\Subscriber;
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
class TablePrefixSubscriber implements \Doctrine\Common\EventSubscriber
{
protected $prefix = '';
public function __construct($prefix)
{
$this->prefix = (string) $prefix;
}
public function getSubscribedEvents()
{
return array('loadClassMetadata');
}
public function loadClassMetadata(LoadClassMetadataEventArgs $args)
{
$classMetadata = $args->getClassMetadata();
if ($classMetadata->isInheritanceTypeSingleTable() && !$classMetadata->isRootEntity()) {
// if we are in an inheritance hierarchy, only apply this once
return;
}
$classMetadata->setTableName($this->prefix . $classMetadata->getTableName());
foreach ($classMetadata->getAssociationMappings() as $fieldName => $mapping) {
if ($mapping['type'] == \Doctrine\ORM\Mapping\ClassMetadataInfo::MANY_TO_MANY
&& array_key_exists('name', $classMetadata->associationMappings[$fieldName]['joinTable']) ) { // Check if "joinTable" exists, it can be null if this field is the reverse side of a ManyToMany relationship
$mappedTableName = $classMetadata->associationMappings[$fieldName]['joinTable']['name'];
$classMetadata->associationMappings[$fieldName]['joinTable']['name'] = $this->prefix . $mappedTableName;
}
}
}
}
Optional step for postgres users: do something similary for sequences
Enjoy
Alternate answer
This is an update taking into account the newer features available in Doctrine2.
Doctrine2 naming strategy
Doctrine2 uses NamingStrategy classes which implement the conversion from a class name to a table name or from a property name to a column name.
The DefaultNamingStrategy just finds the "short class name" (without its namespace) in order to deduce the table name.
The UnderscoreNamingStrategy does the same thing but it also lowercases and "underscorifies" the "short class name".
Your CustomNamingStrategy class could extend either one of the above (as you see fit) and override the classToTableName and joinTableName methods to allow you to specify how the table name should be constructed (with the use of a prefix).
For example my CustomNamingStrategy class extends the UnderscoreNamingStrategy and finds the bundle name based on the namespacing conventions and uses that as a prefix for all tables.
Symfony2 naming strategy
Using the above in Symfony2 requires declaring your CustomNamingStragery class as a service and then referencing it in your config:
doctrine:
# ...
orm:
# ...
#naming_strategy: doctrine.orm.naming_strategy.underscore
naming_strategy: my_bundle.naming_strategy.prefixed_naming_strategy
Pros and cons
Pros:
running one piece of code to do one single task -- your naming strategy class is called directly and its output is used;
clarity of structure -- you're not using events to run code which alter things that have already been built by other code;
better access to all aspects of the naming conventions;
Cons:
zero access to mapping metadata -- you only have the context that was given to you as parameters (this can also be a good thing because it forces convention rather than exception);
needs doctrine 2.3 (not that much of a con now, it might have been in 2011 when this question was asked :-));
Simshaun's answer works fine, but has a problem when you have a single_table inheritance, with associations on the child entity. The first if-statement returns when the entity is not the rootEntity, while this entity might still have associations that have to be prefixed.
I fixed this by adjusting the subscriber to the following:
<?php
namespace MyBundle\Subscriber;
use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
class TablePrefixSubscriber implements EventSubscriber
{
protected $prefix = '';
/**
* Constructor
*
* #param string $prefix
*/
public function __construct($prefix)
{
$this->prefix = (string) $prefix;
}
/**
* Get subscribed events
*
* #return array
*/
public function getSubscribedEvents()
{
return array('loadClassMetadata');
}
/**
* Load class meta data event
*
* #param LoadClassMetadataEventArgs $args
*
* #return void
*/
public function loadClassMetadata(LoadClassMetadataEventArgs $args)
{
$classMetadata = $args->getClassMetadata();
// Only add the prefixes to our own entities.
if (FALSE !== strpos($classMetadata->namespace, 'Some\Namespace\Part')) {
// Do not re-apply the prefix when the table is already prefixed
if (false === strpos($classMetadata->getTableName(), $this->prefix)) {
$tableName = $this->prefix . $classMetadata->getTableName();
$classMetadata->setPrimaryTable(['name' => $tableName]);
}
foreach ($classMetadata->getAssociationMappings() as $fieldName => $mapping) {
if ($mapping['type'] == ClassMetadataInfo::MANY_TO_MANY && $mapping['isOwningSide'] == true) {
$mappedTableName = $classMetadata->associationMappings[$fieldName]['joinTable']['name'];
// Do not re-apply the prefix when the association is already prefixed
if (false !== strpos($mappedTableName, $this->prefix)) {
continue;
}
$classMetadata->associationMappings[$fieldName]['joinTable']['name'] = $this->prefix . $mappedTableName;
}
}
}
}
}
This has a drawback though;
A not wisely chosen prefix might cause conflicts when it's actually already part of a table name.
E.g. using prefix 'co' when theres a table called 'content' will result in a non-prefixed table, so using an underscore like 'co_' will reduce this risk.
Also, you can use this bundle for the new version of Symfony (4) - DoctrinePrefixBundle
I don't when to implement a solution that involved catching event (performance concern), so I have tried the Alternate Solution but it doesn't work for me.
I was adding the JMSPaymentCoreBundle and wanted to add a prefix on the payment tables.
In this bundle, the definition of the tables are in the Resources\config\doctrine directory (xml format).
I have finally found this solution:
1) copy doctrine directory containing the definitions on the table and paste it in my main bundle
2) modify the name of the tables in the definitions to add your prefix
3) declare it in your config.yml, in the doctrine/orm/entity manager/mapping section (the dir is the directory where you have put the modified definitions):
doctrine:
orm:
...
entity_managers:
default:
mappings:
...
JMSPaymentCoreBundle:
mapping: true
type: xml
dir: "%kernel.root_dir%/Resources/JMSPayment/doctrine"
alias: ~
prefix: JMS\Payment\CoreBundle\Entity
is_bundle: false
tested with Symfony 6 :
Create a class that extends Doctrine's UnderscoreNamingStrategy and handles the prefix :
<?php
# src/Doctrine/PrefixedNamingStrategy.php
namespace App\Doctrine;
use Doctrine\ORM\Mapping\UnderscoreNamingStrategy;
class PrefixedNamingStrategy extends UnderscoreNamingStrategy
{
private const PREFIX = 'sf';
public function classToTableName($className)
{
$underscoreTableName = parent::classToTableName($className);
return self::PREFIX . '_' . $underscoreTableName;
}
}
and configure doctrine to use it :
# config/packages/doctrine.yaml
doctrine:
orm:
naming_strategy: 'App\Doctrine\PrefixedNamingStrategy'
#simshaun answer is good, but there is a problem with Many-to-Many relationships and inheritance.
If you have a parent class User and a child class Employee, and the Employee own a Many-to-Many field $addresses, this field's table will not have a prefix.
That is because of:
if ($classMetadata->isInheritanceTypeSingleTable() && !$classMetadata->isRootEntity()) {
// if we are in an inheritance hierarchy, only apply this once
return;
}
User class (parent)
namespace FooBundle\Bar\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* User
*
* #ORM\Entity()
* #ORM\Table(name="user")
* #ORM\InheritanceType("SINGLE_TABLE")
* #ORM\DiscriminatorColumn(name="type", type="string")
* #ORM\DiscriminatorMap({"user" = "User", "employee" = "\FooBundle\Bar\Entity\Employee"})
*/
class User extends User {
}
Employee class (child)
namespace FooBundle\Bar\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* User
*
* #ORM\Entity()
*/
class Employee extends FooBundle\Bar\Entity\User {
/**
* #var ArrayCollection $addresses
*
* #ORM\ManyToMany(targetEntity="\FooBundle\Bar\Entity\Adress")
* #ORM\JoinTable(name="employee_address",
* joinColumns={#ORM\JoinColumn(name="employee_id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="address_id", referencedColumnName="id")}
* )
*/
private $addresses;
}
Address class (relation with Employee)
namespace FooBundle\Bar\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* User
*
* #ORM\Entity()
* #ORM\Table(name="address")
*/
class Address {
}
With the original solution, if you apply pref_ prefixe to this mapping, you will end up with tables :
pref_user
pref_address
employee_address
Solution
A solution can be to modify, in the answer of #simshaun, the point 4 like this:
Create MyBundle\Subscriber\TablePrefixSubscriber.php
<?php
namespace MyBundle\Subscriber;
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
class TablePrefixSubscriber implements \Doctrine\Common\EventSubscriber
{
protected $prefix = '';
public function __construct($prefix)
{
$this->prefix = (string) $prefix;
}
public function getSubscribedEvents()
{
return array('loadClassMetadata');
}
public function loadClassMetadata(LoadClassMetadataEventArgs $args)
{
$classMetadata = $args->getClassMetadata();
// Put the Many-yo-Many verification before the "inheritance" verification. Else fields of the child entity are not taken into account
foreach($classMetadata->getAssociationMappings() as $fieldName => $mapping) {
if($mapping['type'] == \Doctrine\ORM\Mapping\ClassMetadataInfo::MANY_TO_MANY
&& array_key_exists('name', $classMetadata->associationMappings[$fieldName]['joinTable']) // Check if "joinTable" exists, it can be null if this field is the reverse side of a ManyToMany relationship
&& $mapping['sourceEntity'] == $classMetadata->getName() // If this is not the root entity of an inheritance mapping, but the "child" entity is owning the field, prefix the table.
) {
$mappedTableName = $classMetadata->associationMappings[$fieldName]['joinTable']['name'];
$classMetadata->associationMappings[$fieldName]['joinTable']['name'] = $this->prefix . $mappedTableName;
}
}
if($classMetadata->isInheritanceTypeSingleTable() && !$classMetadata->isRootEntity()) {
// if we are in an inheritance hierarchy, only apply this once
return;
}
$classMetadata->setTableName($this->prefix . $classMetadata->getTableName());
}
}
Here we handle the Many-to-Many relationship before verifying if the class is the child of an inheritance, and we add $mapping['sourceEntity'] == $classMetadata->getName() to add the prefix only one time, on the owning entity of the field.

Embed a Collection of Forms Error: Could not determine access type for property

I am trying to embed collection of Tag forms to Service form, according to this tutorial. Tag and Service entities have many-to-many relationship.
Form is rendering correctly. But when I submit form, I get
Could not determine access type for property "tagList"
error. I don't understand why new Tag object is not added to the Service class by calling the addTag() method.
ServiceType
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title', TextType::class, array(
'label' => 'Title'
))
;
$builder->add('tagList', CollectionType::class, array(
'entry_type' => TagType::class,
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false
)));
}
Service class
{
....
/**
* #ORM\ManyToMany(targetEntity="Tag", mappedBy="serviceList",cascade={"persist"})
*/
private $tagList;
/**
* #return ArrayCollection
*/
public function getTagList()
{
return $this->tagList;
}
/**
* #param Tag $tag
* #return Service
*/
public function addTag(Tag $tag)
{
if ($this->tagList->contains($tag) == false) {
$this->tagList->add($tag);
$tag->addService($this);
}
}
/**
* #param Tag $tag
* #return Service
*/
public function removeTag(Tag $tag)
{
if ($this->tagList->contains($tag)) {
$this->tagList->removeElement($tag);
$tag->removeService($this);
}
return $this;
}
}
Tag class
{
/**
* #ORM\ManyToMany(targetEntity="Service", inversedBy="tagList")
* #ORM\JoinTable(name="tags_services")
*/
private $serviceList;
/**
* #param Service $service
* #return Tag
*/
public function addService(Service $service)
{
if ($this->serviceList->contains($service) == false) {
$this->serviceList->add($service);
$service->addTag($this);
}
return $this;
}
/**
* #param Service $service
* #return Tag
*/
public function removeService(Service $service)
{
if ($this->serviceList->contains($service)) {
$this->serviceList->removeElement($service);
$service->removeTag($this);
}
return $this;
}
}
ServiceController
public function newAction(Request $request)
{
$service = new Service();
$form = $this->createForm('AppBundle\Form\ServiceType', $service);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($service);
$em->flush();
return $this->redirectToRoute('service_show', array('id' => $service->getId()));
}
return $this->render('AppBundle:Service:new.html.twig', array(
'service' => $service,
'form' => $form->createView(),
));
}
Could you please try to implement code from this URL?
http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#owning-and-inverse-side-on-a-manytomany-association
First, please try to change mapped/inverse sides, and remove $service->addTag($this); from Tag::addService method.
Short version:
I just ran into this problem and solved it by adding a setter for the affected property:
Could not determine access type for property "tagList"
public function setTagList(Array $tagList)
{
$this->tagList = $tagList;
}
Long version:
The error message is signaling that Symfony is trying to modify the object's state, but cannot figure out how to actually make the change due to the way its class is set up.
Taking a look at Symfony's internals, we can see that Symfony gives you 5 chances to give it access and picks the best one in this order from top to bottom:
A setter method named setProperty() with one argument:
This is the first thing Symfony checks for and is the most explicit way to achieve this. As far as I'm aware this is the best practice:
class Entity {
protected $tagList;
//...
public function getTagList()
{
return $this->tagList;
}
//...
}
A combined getter and setter in one method with one argument:
It's important to realize that this method will also be accessed by Symfony in order to get the object's state. Since those method calls don't include an argument, the argument in this method must be optional.
class Entity {
protected $tagList;
//...
public function tagList($tags = null)
{
if($reps){
$this->tagList = $tags;
} else {
return $this->tagList;
}
}
//...
}
The affected property being declared as public:
class Entity {
public $tagList;
//... other properties here
}
A __set magic method:
This will affect all properties rather than just the one you intended.
class Entity {
public $tagList;
//...
public function __set($name, $value){
$this->$name = $value;
}
//...
}
A __call magic method (in some cases):
I wasn't able to confirm this, but the internal code suggests this is possible when magic is enabled on PropertyAccessor's construction.
Only using one of the above strategies is required.
Maybe the problem is that Symfony can't access that property?
If you look at where that exception is thrown (writeProperty method in the PropertyAccessor class) it says it can be thrown:
If the property does not exist or is not public.
In the tutorial you mentioned it has property $tags, and method addTag. I'm just guessing here, but maybe there's a convention where it tries to call a method names add($singularForm) and this is failing for you because the property is tagList and the method is addTag.
I'm not 100% sure, but you could try debugging by setting a stop point in that Symfony method to see why it's being thrown.
Maybe you forgot in the __construct() of Service class and Tag class to initialize $tagList and $serviceList like this ?
$this->tagList = new ArrayCollection();
$this->serviceList = new ArrayCollection();
This seems like an error with your constructor. Try this :
public function __construct()
{
$this-> tagList = new \Doctrine\Common\Collections\ArrayCollection();
}
It's a long shot, but looking at your annotations I think the problem might be related to your manyToMany relationship. Try to change the owning side and inverse side (Swap the relationship) unless you specifically need to update from both ends (In that case I think the only solution is to add the objects manually or use oneToMany relationships).
Changes made only to the inverse side of an association are ignored.
Make sure to update both sides of a bidirectional association (or at
least the owning side, from Doctrine’s point of view)
This is a problem related to Doctrine I have suffered before, see:
http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/unitofwork-associations.html
Based on Symfony 3.3.10
I actually faced this problem many and many times, finally once i discovered where this problem was coming from, depending on the name you give to your entity property it can happen that the adder and the remover for your collection property aren't exactly what you are expecting.
Example: Your entity properity name is "foo" and you would expect the adder to be called "addFoo" and remover "removeFoo", but then all of a sudden the "Could not determine access type for property" appear.
So you start going into fear searching for w/e problems in your code, instead you just have to look this file inside Symfony core files:
vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php
Inside this file there's a method called findAdderAndRemover.
Go there with your debugger and you will eventually find out that symfony searches for weird name for your adder/remover, they may actually end with "um" or "on" or "us" depending on the language (human language) you used to name them. Since i'm Italian this happen quite often.
Watch out for that, since the fix may be as simple as changing the name used for your add/remove method inside your entity to make them match with what Symfony core is looking for.
This happens to me when i use bin/console doctrine:generate:entities to create the methods automatically for me
If you are using symfony, and use EntityRepository instead of CollectionType, make sure you use the 'multiple' => true, on your form build, otherwise the input will be for one entity and not for many, therefore it will call the setTagList instead of using the methods addTagList and removeTagList.

How to correctly test repository with xSpec tool?

I learn TDD and I've started to use xSpec tool (language does not matter, but it's phpspec2 in my case). I write my first specification:
<?php
namespace spec\Mo\SpeechBundle\Controller;
use Doctrine\Common\Collections\Collection;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Component\HttpFoundation\Response;
use Mo\SpeechBundle\Repository\IdeaRepository;
use Mo\SpeechBundle\Repository\SpeechRepository;
use Mo\SpeechBundle\Entity\Idea;
use Mo\SpeechBundle\Entity\Speech;
class SpeechControllerSpec extends ObjectBehavior
{
function let(SpeechRepository $speechRepository, IdeaRepository $ideaRepository, EngineInterface $templating)
{
$this->beConstructedWith($speechRepository, $ideaRepository, $templating);
}
function it_is_initializable()
{
$this->shouldHaveType('Mo\SpeechBundle\Controller\SpeechController');
}
function it_responds_to_show_action(EngineInterface $templating, Speech $speech, Response $response)
{
$templating
->renderResponse('MoSpeechBundle:Speech:show.html.twig', ['speech' => $speech])
->willReturn($response)
;
$this->showAction($speech)->shouldBeAnInstanceOf('Symfony\Component\HttpFoundation\Response');
}
function it_responds_to_list_action(
SpeechRepository $speechRepository,
IdeaRepository $ideaRepository,
EngineInterface $templating,
Response $response
)
{
$speeches = [new Speech()];
$ideas = [new Idea()];
$speechRepository->findAll()->willReturn($speeches);
$ideaRepository->findAll()->willReturn($ideas);
$templating
->renderResponse('MoSpeechBundle:Speech:list.html.twig', compact('speeches', 'ideas'))
->willReturn($response)
;
$this->listAction()->shouldBeAnInstanceOf('Symfony\Component\HttpFoundation\Response');
}
function it_responds_list_by_idea_action(
Idea $idea,
SpeechRepository $speechRepository,
IdeaRepository $ideaRepository,
EngineInterface $templating,
Response $response
)
{
$speeches = [new Speech()];
$ideas = [new Idea()];
$speechRepository->findByIdea($idea)->willReturn($speeches);
$ideaRepository->findAll()->willReturn($ideas);
$templating
->renderResponse('MoSpeechBundle:Speech:list.html.twig', compact('speeches', 'idea', 'ideas'))
->willReturn($response)
;
$this->listByIdeaAction($idea)->shouldBeAnInstanceOf('Symfony\Component\HttpFoundation\Response');
}
}
For controller:
<?php
namespace Mo\SpeechBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Mo\SpeechBundle\Repository\IdeaRepository;
use Mo\SpeechBundle\Repository\SpeechRepository;
use Mo\SpeechBundle\Entity\Idea;
use Mo\SpeechBundle\Entity\Speech;
/**
* Manages speeches.
*/
class SpeechController
{
/**
* #var \Mo\SpeechBundle\Repository\SpeechRepository
*/
private $speechRepository;
/**
* #var \Mo\SpeechBundle\Repository\IdeaRepository
*/
private $ideaRepository;
/**
* #var \Symfony\Bundle\FrameworkBundle\Templating\EngineInterface
*/
private $templating;
/**
* #param \Mo\SpeechBundle\Repository\SpeechRepository $speechRepository
* #param \Mo\SpeechBundle\Repository\IdeaRepository $ideaRepository
* #param \Symfony\Bundle\FrameworkBundle\Templating\EngineInterface $templating
*/
public function __construct(SpeechRepository $speechRepository, IdeaRepository $ideaRepository, EngineInterface $templating)
{
$this->speechRepository = $speechRepository;
$this->ideaRepository = $ideaRepository;
$this->templating = $templating;
}
/**
* Shows speech.
*
* #param \Mo\SpeechBundle\Entity\Speech $speech
*
* #return \Symfony\Component\HttpFoundation\Response
*/
public function showAction(Speech $speech)
{
return $this->templating->renderResponse('MoSpeechBundle:Speech:show.html.twig', compact('speech'));
}
/**
* Shows list of speeches filtered by idea.
*
* #param \Mo\SpeechBundle\Entity\Idea $idea
*
* #return \Symfony\Component\HttpFoundation\Response
*/
public function listByIdeaAction(Idea $idea)
{
$speeches = $this->speechRepository->findByIdea($idea);
$ideas = $this->ideaRepository->findAll();
return $this->templating->renderResponse('MoSpeechBundle:Speech:list.html.twig', compact('speeches', 'idea', 'ideas'));
}
/**
* Shows list of all speeches.
*
* #return \Symfony\Component\HttpFoundation\Response
*/
public function listAction()
{
$speeches = $this->speechRepository->findAll();
$ideas = $this->ideaRepository->findAll();
return $this->templating->renderResponse('MoSpeechBundle:Speech:list.html.twig', compact('speeches', 'ideas'));
}
}
OK, now I'm sure that behavior of my controller is specified and I can move forward. But I have another problem.
I've used mock of repository for controller spec and now I want to write spec for repository itself:
<?php
namespace Mo\SpeechBundle\Repository;
use Doctrine\ORM\EntityRepository;
use Mo\SpeechBundle\Entity\Idea;
class SpeechRepository extends EntityRepository
{
/**
* Finds all speeches by specified idea.
*
* #param \Mo\SpeechBundle\Entity\Idea $idea
*
* #return array
*/
public function findByIdea(Idea $idea)
{
return $this
->createQueryBuilder('s')
->leftJoin('s.ideas', 'i')
->where('i = :idea')
->setParameters(compact('idea'))
->getQuery()
->getResult()
;
}
}
But specs describe behavior, as I understood. How correctly test repository that it really returns what I need, in my case speeches by ideas.
Should I consider to create functional test with xUnit tool (PHPUnit in PHP world)? Or I wri
te spec which describes that my repository correctly creates query? Or can I just use Behat for all app and don't pay attention to this problem.
I have spent a week for analysis of this question and found satisfactory answer.
The phpspec only specify behavior of our objects. Nothing more. We can't create functional tests with them.
So, we have two ways to test our functionality:
Use PHPUnit to write functional tests for modules and system itself.
Use Behat to describe features of our application.
PHPUnit, other similar framework and Behat have their pitfalls and strong sides.
What to use, can decide only a developer.
I understand your predicament completely and I have done exactly the same thing in the past. I think that the basic answer to your question is that your business logic should be separate from any framework (infrastructure code), and so you should not be testing objects of the type 'Doctrine\ORM\EntityRepository'.
I think the best approach is the have another layer within your application which will hold your business logic, and this in turn could use adapters to pass messages back and forth from the 'Doctrine\ORM\EntityRepository' type objects. That way you can fully spec your business rules (including any adapters) without having to test doctrine type objects which should not be tested anyway as for the most part this is 3rd party code.
Doing things this way also makes it easier for you to change your business rules in future.

Symfony2 multiple #template in a controller

I am very new to symfony2 and trying very hard for past few days to figure out a problem. My controller is
<?php
namespace Myname\AdminBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
class DefaultController extends Controller
{
/**
* #Route("/admin/{type}/{nav}", defaults={"nav"="nil"})
* #Template("MyBundle::index.html.twig")
*/
public function indexAction($type, $nav)
{
return array('type' => $type, 'nav' => $nav);
}
/**
* #Route("/myLink/accounts", name="page_accounts")
* #Template("MyBundle::accounts.html.twig")
*/
public function accountsAction()
{
return $this->render('MyBundle::accounts.html.twig');
}
}
But the problem I am facing is, which ever link I am trying to use, it is using the first defined template and the second template is never rendered. But if I remove the first template from the code, the second one is working perfectly. Any comments will be really great as I am stuck for last 4 days. Thanks in advance.
I am not sure I fully understand your issue. However, I think there are some issues with your controller and how you are using the #Template annotation.
First, if you are following the default conventions, then you don't even need to specify the template to use. Also, when using the #Template annotation, you are supposed to return an array.
If none of this helps or is related, could you please clarify some more?
class DefaultController extends Controller
{
/**
* #Route("/admin/{type}/{nav}", defaults={"nav"="nil"})
* #Template
*/
public function indexAction($type, $nav)
{
return array('type' => $type, 'nav' => $nav);
}
/**
* #Route("/myLink/accounts", name="page_accounts")
* #Template
*/
public function accountsAction()
{
return array();
}
}

Doctrine 2 Can't Seem to Remove Many to Many Relationships

I have the following setup "Many Users can have Many Projects (Collaborators)"
/**
* #Entity #HasLifeCycleCallbacks
* #Table(name="projects")
*/
class Project implements \Zend_Acl_Resource_Interface {
/**
* #ManyToMany(targetEntity="User", mappedBy="projects")
* #OrderBy({"displayName" = "ASC", "username" = "ASC"})
*/
protected $collaborators;
..
}
/**
* #Entity
* #Table(name="users")
*/
class User implements \Zend_Acl_Role_Interface {
/**
* #ManyToMany(targetEntity="Project", inversedBy="collaborators")
*/
protected $projects;
...
}
I tried to remove a collaborator using the following
$user = Application_DAO_User::findById($this->_getParam('userid'));
$proj = Application_DAO_Project::getProjectById($this->_getParam('id'));
Application_DAO_Project::removeCollaborator($proj, $user); // <---
// Application_DAO_User
public static function findById($id) {
return self::getStaticEm()->find('Application\Models\User', $id);
}
// Application_DAO_Project
public static function getProjectById($id) {
return self::getStaticEm()->find('Application\Models\Project', $id);
}
public static function removeCollaborator(Project $proj, User $collaborator) { // <---
$proj->getCollaborators()->remove($collaborator);
$collaborator->getProjects()->remove($proj);
self::getStaticEm()->flush();
}
And there isn't any errors but the database stays the same ...
This may be well over due but was just experiencing the same problem myself... According to the doctrine 2 documents, the function ArrayCollection->remove($i) is for removing by array index.
What you are after is:
getCollaborators()->removeElement($collaborator);
I went round in circles trying to figure this out until I realised that for this to work:
getCollaborators()->removeElement($collaborator);
$collaborator would have to be the actual object from the collaborators ArrayCollection. That is, if you pass in a new Collaborator object with the same parameters it won't remove it. That's because ArrayCollection uses array_search to look for the object you want to remove.
Hope that saves someone else a few hours...