标签: doctrine-orm

如何调试MySQL/Doctrine2查询?

我正在使用MySQL与Zend Framework和Doctrine 2.我认为即使你不使用Doctrine 2,你也会熟悉像

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ASC' at line 1

问题是我没有看到完整的查询.没有ORM框架,我可能很容易回应sql,但是使用框架,我怎样才能找到它试图执行的SQL?我把错误缩小到了

$progress = $task->getProgress();
Run Code Online (Sandbox Code Playgroud)

$progress 被宣布

// Application\Models\Task
/**
 * @OneToMany(targetEntity="TaskProgress", mappedBy="task")
 * @OrderBy({"seq" = "ASC"})
 */
protected $progress;
Run Code Online (Sandbox Code Playgroud)

在MySQL中,任务类看起来像

CREATE TABLE `tasks` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `owner_id` int(11) DEFAULT NULL,
  `assigned_id` int(11) DEFAULT NULL,
  `list_id` int(11) DEFAULT …
Run Code Online (Sandbox Code Playgroud)

php mysql zend-framework doctrine-orm

24
推荐指数
4
解决办法
5万
查看次数

从doctrine 2中的datetime对象获取日期作为字符串

在我的一个实体中,我有一个被称为受保护的属性insert_date,它是一个日期时间.

当我之后提取数据时,我没有将日期作为字符串,我得到一个对象.我的var转储:

<pre class='xdebug-var-dump' dir='ltr'> <b>object</b>(<i>DateTime</i>)[<i>1560</i>] <i>public</i> 'date' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'2011-08-26 12:40:29'</font> <i>(length=19)</i> <i>public</i> 'timezone_type' <font color='#888a85'>=&gt;</font> <small>int</small> <font color='#4e9a06'>3</font> <i>public</i> 'timezone' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'Europe/London'</font> <i>(length=13)</i> </pre><pre class='xdebug-var-dump' dir='ltr'> <b>object</b>(<i>DateTime</i>)[<i>1571</i>] <i>public</i> 'date' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'2011-08-26 12:40:29'</font> <i>(length=19)</i> <i>public</i> 'timezone_type' <font color='#888a85'>=&gt;</font> <small>int</small> <font color='#4e9a06'>3</font> <i>public</i> 'timezone' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'Europe/London'</font> <i>(length=13)</i>
Run Code Online (Sandbox Code Playgroud)

我试过了:

foreach($dateObj as $date) {

}
Run Code Online (Sandbox Code Playgroud)

但是它没有提取......我如何从这个对象中获取日期属性?即使$insert_date->date不起作用.

php datetime doctrine doctrine-orm

24
推荐指数
2
解决办法
5万
查看次数

我可以在doctrine2中从php访问discriminator字段吗?

我有一个定义继承的实体,如下所示:

* @DiscriminatorColumn(name="type", type="string")
* @DiscriminatorMap({"text" = "TextAttribute", "boolean" = "BooleanAttribute", "numeric" = "NumericAttribute", "date" = "DateAttribute"})
Run Code Online (Sandbox Code Playgroud)

我想知道是否有可能为田地'类型'吸气?我知道我可以使用instanceof(在大多数情况下这就是我正在做的事情)但是很少有场景,$ item-> getType()会让我的生活变得如此简单.

php inheritance doctrine doctrine-orm

23
推荐指数
5
解决办法
2万
查看次数

具有多个索引的主义2

我正在使用zend框架和doctrine2.1进行开发.

我从数据库生成了实体.

但问题是:Doctrine无法识别我的索引.它们根本没有在实体注释中标记.

当我去验证模式并从中转储sql时orm:schema-tool:update --dump-sql,生成sql以删除整个数据库中的所有索引.

我发现Doctrine有以下用于定义索引的注释:

indexes={@index(name="index_name",
                columns={"database_column1","database_column2"}
        )}
Run Code Online (Sandbox Code Playgroud)

但这允许我为多列定义一个索引,我真的不需要它.
我想要的是能够在多列上定义多个索引,每列一个索引.

有没有办法实现这个目标?有没有一种方法可以让我有一个定义多个索引的注释.

mysql indexing annotations doctrine-orm

23
推荐指数
2
解决办法
1万
查看次数

如何在symfony2项目上使用sqlite数据库?

在Symfony2项目中,您可以在app/config/parameters.ini文件中配置数据库连接.文档说明您可以使用sqlite3 PDO驱动程序.

但配置sqlite不能很好地工作:

[parameters]
    database_driver   = pdo_sqlite
    database_host     = localhost
    database_port     =
    database_name     = test_project.db
    database_user     = root
    database_password = 
Run Code Online (Sandbox Code Playgroud)

使用app/console doctrine:database:create,在项目根目录下成功创建test_project.db文件.

但是在创建了一些实体之后,然后运行app/console doctrine:schema:update --force应该在数据库文件上创建表,但它没有,文件显示为空,O字节大小.

请注意,使用任何其他PDO驱动程序效果很好,但不能使用SQLite ...

我还尝试在database_name参数中使用db文件的完整路径,但无济于事,数据库仍然没有得到更新.

作为参考,这是config.yml文件的doctrine dbal部分:

doctrine:
    dbal:
        driver:   %database_driver%
        host:     %database_host%
        port:     %database_port%
        dbname:   %database_name%
        user:     %database_user%
        password: %database_password%
        charset:  UTF8
Run Code Online (Sandbox Code Playgroud)

有没有解决的办法?配置缺失?在symfony2项目的官方文档中没有说明的内容?

sqlite symfony doctrine-orm

23
推荐指数
2
解决办法
4万
查看次数

有没有办法为Doctrine 2模型指定默认顺序?

在Rails中(甚至在Doctrine <2,IIRC中),您可以为任何模型指定默认顺序.例如,如果你告诉Rails总是命令你customer的表name,Customer.all将永远被客户订购的列表name.它具有很大的意义.

从我收集的内容来看,它无法在Doctrine 2中执行此操作.显然,他们希望您创建一个查询.

在我看来,这将是一个非常干燥,逻辑和方便的功能,并且选择遗漏的一个非常愚蠢的功能.

我真诚地希望我错误地认为这个选项不存在,在我今晚哭泣自己睡觉之前,我想检查一下Doctrine是否真的有办法指定一个默认订单我只是无法找到它.任何人都可以开导我吗?

sorting doctrine-orm

23
推荐指数
5
解决办法
3万
查看次数

使用与多个实体经理的关系

我想知道是否有可能在驻留在不同数据库中的两个实体之间创建关系.

例如,如果我们采用http://symfony.com/doc/current/cookbook/doctrine/multiple_entity_managers.html中找到的解决方案,并在客户数据库中创建与用户的一对多关系到默认数据库中的帖子.

这是Symfony2和Doctrine支持的东西吗?

symfony doctrine-orm

23
推荐指数
1
解决办法
2万
查看次数

如何在`collection`字段Symfony 2.1中将选项传递给CustomType?

我有SuperType实体表格Super.

在这个表单中,我有一个实体collectionChildType表单类型字段Child

class SuperType:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('childrens', 'collection', array(
            'type' => new ChildType(null, array('my_custom_option' => true)),  
}
Run Code Online (Sandbox Code Playgroud)

class ChildType:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    if ($options['my_custom_option']) {
        $builder->add('my_custom_field', 'textarea'));
    }
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
  $resolver->setDefaults(array(
      ...
      'my_custom_option' => false
  ));
}
Run Code Online (Sandbox Code Playgroud)

如何my_custom_option仅为此SuperType表单更改值?

当然,我尝试通过构造函数传递此选项不起作用.

php forms symfony doctrine-orm

23
推荐指数
2
解决办法
1万
查看次数

表已经存在

我在zend框架2中使用doctrine 2.下面是我的实体文件.问题是,当我尝试使用时验证模式,

./vendor/bin/doctrine-module orm:validate-schema
Run Code Online (Sandbox Code Playgroud)

命令.

我收到了错误,

[Doctrine\DBAL\Schema\SchemaException]                               
The table with name 'database.opportunitycriteria' already exists.
Run Code Online (Sandbox Code Playgroud)

我该怎么办?

namespace Administration\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * OpportunityCriteria
 *
 * @ORM\Table(name="OpportunityCriteria")
 * @ORM\Entity
 */
class Criteria
{
/**
 * @var integer
 * @ORM\Id
 * @ORM\Column(name="criteria_id", type="integer", nullable=false)
 */
private $criteria_id;

/**
 * @var string
 *
 * @ORM\Column(name="description", type="string", nullable=false)
 */
private $description;
}
Run Code Online (Sandbox Code Playgroud)

和适当的getter和setter方法..

doctrine-orm zend-framework2

23
推荐指数
1
解决办法
7904
查看次数

Symfony2形成事件和模型变换器

我正试图与Symfony2的表格制作者,活动和变形金刚搏斗...希望有人在这里更有经验,可以提供帮助!

我有一个表单字段(选择下拉列表),其中包含一些映射到实体的值(一个候选名单).其中一个选项是"其他".假设现在没有AJAX,当用户提交表格我想要检测他们是否选择了"其他"(或者不在候选名单中的任何其他选项).如果他们选择了其中一个选项,则应显示完整的选项列表,否则只显示候选名单.应该很容易吧?;)

所以,我有我的表格类型,它显示基本的候选名单就好了.代码看起来像这样:

namespace Company\ProjectBundle\Form\Type;

use ...

class FancyFormType extends AbstractType {
    private $fooRepo;

    public function __construct(EntityManager $em, FooRepository $fooRepo)
    {
        $this->fooRepo = $fooRepo;
    }

    public function buildForm(FormBuilderInterface $builder, array $options) {
        /** @var Bar $bar */
        $bar = $builder->getData();
        $fooTransformer = new FooToStringTransformer($options['em']);

        $builder
            ->add($builder
                ->create('linkedFoo', 'choice', array(
                    'choices' => $this->fooRepo->getListAsArray(
                        $bar->getLinkedfoo()->getId()
                    ),
                ))
                ->addModelTransformer($fooTransformer)
            )
        ;

        // ...

    }

    // ...
}
Run Code Online (Sandbox Code Playgroud)

现在,我想检查提交的值,因此我使用Form Event Listener,如下所示.

public function buildForm(FormBuilderInterface $builder, array $options) {
    // ... This code comes just …
Run Code Online (Sandbox Code Playgroud)

php forms symfony doctrine-orm

23
推荐指数
1
解决办法
2万
查看次数