- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
对于 Symfony2 项目,我必须在博客文章和所谓的平台之间建立关系。平台根据您用于查看站点的域定义特定过滤器。例如:如果您通过 url first-example.com 加入该站点,该站点将仅提供连接到该特定平台的博客文章。
为此,我创建了两个实体 Post 和 Platform。之后我用多对多关系将它们映射在一起。我正在尝试通过这种多对多关系从 Doctrines 的 EntityRepository
中的内置函数 findBy()
检索数据。
// every one of these methods will throw the same error
$posts = $postRepo->findBy(array('platforms' => array($platform)));
$posts = $postRepo->findByPlatforms($platform);
$posts = $postRepo->findByPlatforms(array($platform));
其中 $postRepo
是 Post
实体的正确存储库,$platform
是现有的 Platform
对象。
无论哪种方式:我最终都会收到以下错误:
ErrorException: Notice: Undefined index: joinColumns in [...]/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php line 1495
[...]/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php:1495
[...]/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php:1452
[...]/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php:1525
[...]/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php:1018
[...]/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php:842
[...]/vendor/doctrine/orm/lib/Doctrine/ORM/EntityRepository.php:157
[...]/src/Foobar/BlogBundle/Tests/ORM/PostTest.php:102
是否可以通过这种方式检索多对多关系中的相关实体,或者我是否被迫自己编写这些函数?奇怪的是:Doctrine 不会抛出任何错误,如:“这是不可能的。”,而是一个内部的 E_NOTICE
。这就是为什么我倾向于认为它应该是可能的,但我在这里遗漏了一些要点。
剥离到有趣的部分,这两个实体看起来像这样。
<?php
namespace Foobar\CommunityBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
// [...] other namespace stuff
/**
* @ORM\Entity(repositoryClass="Foobar\CommunityBundle\Entity\Repository\PlatformRepository")
* @ORM\Table(name="platforms")
*/
class Platform
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
// [...] other field stuff
}
<?php
namespace Foobar\BlogBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
// [...] other namespace stuff
/**
* @ORM\Entity(repositoryClass="Foobar\BlogBundle\Entity\Repository\PostRepository")
* @ORM\Table(name="posts")
*/
class Post implements Likeable, Commentable, Taggable, PlatformAware
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\ManyToMany(targetEntity="Foobar\CommunityBundle\Entity\Platform", cascade={"persist"})
* @ORM\JoinTable(name="map_post_platform",
* joinColumns={@ORM\JoinColumn(name="post_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="platform_id", referencedColumnName="id")}
* )
*/
protected $platforms;
// [...] other fields
/**
* Constructor
*/
public function __construct()
{
// [...]
$this->platforms = new ArrayCollection();
}
}
当然还有 composer.json 文件(以及相关行)
{
[...]
"require": {
"php": ">=5.3.3",
"symfony/symfony": "2.1.*",
"doctrine/orm": ">=2.2.3,<2.4-dev",
"doctrine/doctrine-bundle": "1.0.*",
"doctrine/doctrine-fixtures-bundle": "dev-master",
[...]
},
[...]
}
最佳答案
另一种方式,可能有点 OO/cleaner 而不使用 ID:
public function getPosts(Platform $platform)
{
$qb = $this->createQueryBuilder("p")
->where(':platform MEMBER OF p.platforms')
->setParameters(array('platform' => $platform))
;
return $qb->getQuery()->getResult();
}
更好的方法名称是 findPostsByPlatform
关于php - 在多对多关系中使用 EntityRepository::findBy() 将导致 Doctrine 中的 E_NOTICE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13343533/
我有一些类(class) Activity : @Data @Document(collection = "event") public class Event { private String id
我有一个带有字符串名称字段的 Book 实体。使用 Spring Data JPA 关键字时有什么区别: Optional findByName(String name); 和 Optional fi
评分细则和品牌表(Many rubric to One Brand)之间存在关系。 这是我的代码 $dbChoices = $this->getConfigurationPool()
我有以下代码: println "@@@@@@@@ RUNNING ProfessionaCustomer - ${pcCounter} under ${accountCustomer.custome
我的Grails应用程序中提供以下服务: class Person { String name } class Host { String url } 然后,我有一个由多个并发线程调用
给定: controller PersonController with a @Transactional action save 服务 PersonService,方法 populateProper
我将 Symfony2 与 Doctrine2 一起使用。有以下实体: /** * Person * * @ORM\Entity(repositoryClass="Acme\Bundle\Con
我有一个包含大约 30 万行描述 Apple 推送通知服务设备的表。我使用 Doctrine 2 作为 ORM。 插入设备没有问题,但是,检索它们是一个完全不同的故事。使用简单的 MySQL SELE
我已经调试了好几天了。 Store.findBy(function (record, id)不行为。或者也许是我行为不端。已将 JSON 放入代码中,以便于测试。 FindBy() 匹配 12345
我有一个扩展,我想在其中包含一些过滤器,我知道我可以使用 findBy 过滤我的 listAction() 显示的结果>. 我测试了它,它是这样工作的: $cars = $this->carRepos
我有一个关于 symfony Doctrine 的片段,它按降序选择数据。尝试应用 where 子句某些字段等于 true 是一个问题。下面是我的片段 $results = $this->getDoc
我想要这个功能 $entityManager ->getRepository('AppBundle:Example') ->findBy(array('id' => $id, 'active' =>
我在表中有一些错误数据,但并非所有字段都具有空值,如果可能的话,我想通过实现类似这样的方法在一次迭代中获取所有错误行。 $contactObj = $this->em->getRepository("
我有域类 => 多对多并使用 addTo 将它们关联起来。 class Books { static belongsTo = Author } class Author { stati
我有一个对象,我必须从中过滤某些属性,其中一些也可能是“空”。我有一个 Filter 对象和一个 Product 对象。 在 Filter 对象中,我有一些反射(reflect) Product 对象
有人在 Symfony 3(最后一个版本)中遇到过这个奇怪的问题吗? 我有以下简单的代码: $repository = $this->getDoctrine()
我遵循在 Doctrine 模型类中设置自定义 findOneByJoinedToCategory($id) 函数的示例,如此处文档中所述: http://symfony.com/doc/curren
我有一个实体 Items,它与另一个实体 ExceptionType 上的字段具有 ManyToOne 关系。我想返回基于银行号码(来自 Items 实体)和 itemExceptionType(来自
所以我有一个有趣的难题,我很想从其他 Webdriver 框架架构师那里得到一些反馈。目前我遵循一个非常标准的执行模型: 基础对象 页面对象(扩展基础对象) Junit 测试对象(引用一个或多个页面对
我有一个 Item 类,其属性名为vendor_name,如下所示: @Entity @Table(name="item_info") public class Item { @Column(
我是一名优秀的程序员,十分优秀!