我假设我需要构建一个本机查询来使用Doctine2截断表.
$emptyRsm = new \Doctrine\ORM\Query\ResultSetMapping();
$sql = 'TRUNCATE TABLE Article';
$query = em()->createNativeQuery($sql, $emptyRsm);
$query->execute();
Run Code Online (Sandbox Code Playgroud)
这给出了错误
SQLSTATE[HY000]: General error
Run Code Online (Sandbox Code Playgroud)
我需要更改为我的代码才能使其工作?
我有一个类别包含这个:
/**
* @ORM\OneToMany(targetEntity="Friend", mappedBy="category")
* @ORM\OrderBy({"name" = "ASC"})
*/
protected $friends;
Run Code Online (Sandbox Code Playgroud)
和一个朋友这个:
/**
* @ORM\ManyToOne(targetEntity="Category", inversedBy="friends")
* @ORM\JoinColumn(name="category_id", referencedColumnName="id", onDelete="SET NULL")
*/
protected $category;
Run Code Online (Sandbox Code Playgroud)
我想要的是能够删除类别,无论是否有这个类别的朋友,如果有 - 这个朋友的类别字段被设置为NULL.
我尝试onDelete="CASCADE"使用ManyToOne注释,然后到OneToMany,我尝试了上面显示的内容,我尝试cascade={"remove"}在OneToMany注释中使用,没有任何效果!我也找不到一个例子.请你帮助我好吗?
我的Symfony2存储库类中有以下代码...
$query = $this->createQueryBuilder('foo')
->where('foo.bar = :id')
->setParameter('id', $myID)
->getQuery();
Run Code Online (Sandbox Code Playgroud)
如何获取数据库找到的行数?
提前致谢
有没有办法确定参数是否是Doctrine已经持久化的对象?类似于实体管理器方法的东西,它检查对象不是普通的旧对象,但实际上已存在于内存中的东西/持久化.
<?php
public function updateStatus(Entity $entity, EntityStatus $entityStatus)
{
$entityManager = $this->getEntityManager();
try {
// checking persisted entity
if (!$entityManager->isPersisted($entity)) {
throw new InvalidArgumentException('Entity is not persisted');
}
// ...
} catch (InvalidArgumentException $e) {
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试将doctrine安装到我的项目中.我收到错误的PHP版本的错误.可以采取哪些措施来消除此错误的真正原因?克服它的方法是使用https://getcomposer.org/doc/03-cli.md#require所述的选项" --ignore-platform-reqs" .
PHP版本:PHP 5.6.18
PHP 5.6.18 (cli) (built: Feb 3 2016 17:20:21)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies
Run Code Online (Sandbox Code Playgroud)
错误:c ..> composer需要doctrine/data-fixture
Using version ^1.1 for doctrine/data-fixtures
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.
Problem 1
- doctrine/migrations v1.3.0 requires …Run Code Online (Sandbox Code Playgroud) 我正在为电子商务应用程序设计我的数据库/域,而我很难弄清楚如何存储产品.
该网站将出售各种产品,钢笔,丁字裤,纹身,雨伞,应有尽有.这些产品中的每一个都会分享一些常见的属性,高度,宽度,长度,重量等,但有些产品有特殊数据.例如,笔具有不同的墨水颜色,并且提示/盖子和小册子可以具有不同类型的折叠.到目前为止,我已经考虑了20多个额外属性,但这些属性可能仅适用于网站上1%的产品.
所以我想知道是否适合实施EAV模型来处理额外的数据.请记住,当客户在前端查看网站时,会有一个过滤侧边栏,如eBay和carsales.com.au.(所以记住会有一些相当多的查询)
我认为实现类表继承是不切实际的,因为系统需要保持灵活性.这是因为,在未来的轨道上,我们可能会在未来使用新类型的产品时拥有更多属性.
我考虑的另一件事是使用NoSQL数据库(可能是MongoDB)但是我对这些类型的数据库没什么经验,它甚至可以解决我的问题吗?
审查选项:
我正在构建一个带有属性实体的原型,以查看它的灵活性,测试性能以及查询失控的方式.
编辑:我当然对任何其他解决方案持开放态度.
php database-design magento entity-attribute-value doctrine-orm
我有一个产品类,其上有许多字段,用于ManyToMany,例如成分,大小,种类等.共有大约14个不同的字段并非所有字段都与每个产品相关.
我有这样的映射设置
Class product {
/**
* @var Species[]
* @ORM\ManyToMany(targetEntity="Species")
* @ORM\JoinTable(name="product_species",
* joinColumns={@ORM\JoinColumn(name="productId", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="speciesId", referencedColumnName="id")}
* )
* @ORM\OrderBy({"name" = "asc"})
*/
private $species;
Run Code Online (Sandbox Code Playgroud)
这适用于许多人/多人.
问题出在我的product_ingredients表中我需要添加一个额外的字段,这意味着需要从ManyToMany切换到OneToMany/ManyToOne所以就像这样
/**
* @var ProductIngredient[]
*
* @ORM\OneToMany(targetEntity="ProductIngredient", mappedBy="product")
* @ORM\JoinColumn(name="productId", referencedColumnName="id")
*/
private $ingredients;
Run Code Online (Sandbox Code Playgroud)
现在我的ProductIngredient实体看起来像这样
/**
* @var IngredientType
* @ORM\ManyToOne(targetEntity="IngredientType", fetch="EAGER")
* @ORM\JoinColumn(name="ingredientTypeId", referencedColumnName="id")
*/
private $ingredientType;
/**
* @var Ingredient
*
* @ORM\ManyToOne(targetEntity="Ingredient", inversedBy="products", fetch="EAGER")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="ingredientId", referencedColumnName="id")
* })
*/
private $ingredient;
/**
* @var …Run Code Online (Sandbox Code Playgroud) 如何从具有最新Symfony和Doctrine的控制器中获取实体管理器?
"The Book"中描述的方式现已标记为已弃用.什么是现代(适当)的方式来做到这一点?
public function someAction()
{
// getEntityManager() from Doctrine\Bundle\DoctrineBundle\Registry is deprecated
$entityManager = $this->getDoctrine()->getEntityManager();
...
}
Run Code Online (Sandbox Code Playgroud) Uncaught exception 'Doctrine\ORM\Mapping\MappingException' with message 'Class "Users" is not a valid entity or mapped super class每次运行下一个代码时都会出现异常:
test.php的
<?php
require_once "vendor/autoload.php";
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
$paths = array(dirname(__FILE__)."/entities");
$isDevMode = false;
// the connection configuration
$dbParams = array(
'driver' => 'pdo_mysql',
'user' => 'root',
'password' => 'pass',
'dbname' => 'snabcentr',
);
$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
$em = EntityManager::create($dbParams, $config);
$user = $em->find("Users", 5);
Run Code Online (Sandbox Code Playgroud)
实体/ Users.php
<?php
use Doctrine\ORM\Mapping as ORM;
/**
* Users
*
* @ORM\Table(name="users")
* @ORM\Entity
*/
class …Run Code Online (Sandbox Code Playgroud) 我正在尝试将我的PHP应用程序迁移到Ubuntu服务器,但没有成功.任何帮助,将不胜感激.
首先,我按照Doctrine 入门手册的第一部分(直到"生成数据库模式")将Doctrine成功安装到/ jorrit/myapp中.其次,我将我的PHP脚本(使用Doctrine)放在文件夹/ jorrit/myapp中.
当我尝试在CLI中运行我的PHP脚本时,收到以下错误消息:
PHP警告:require(/ tmp/__ CG__Source.php):无法打开流:第200行/jorrit/myapp/vendor/doctrine/common/lib/Doctrine/Common/Proxy/AbstractProxyFactory.php中没有此类文件或目录
PHP致命错误:require():在/ jorrit/myapp/vendor/doctrine /中打开所需的'/tmp/__CG__Source.php'(include_path ='.:/ usr/share/php:/ usr/share/pear')失败第200行的common/lib/Doctrine/Common/Proxy/AbstractProxyFactory.php
Bootstrap.php看起来像这样:
<?php
// bootstrap.php
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
require_once "vendor/autoload.php";
// Create a simple "default" Doctrine ORM configuration for Annotations
$isDevMode = false;
$config = Setup::createAnnotationMetadataConfiguration(array(__DIR__."/src"), $isDevMode);
// the connection configuration
$dbParams = array(
'driver' => 'pdo_mysql',
'host' => 'xx',
'user' => 'xx',
'password' => 'xx',
'dbname' => 'xx',
'profiler' => 'false'
);
// obtaining the entity manager
$entityManager = EntityManager::create($dbParams, $config);
?>
Run Code Online (Sandbox Code Playgroud)
我的PHP脚本的第一行: …
doctrine-orm ×10
php ×7
symfony ×5
composer-php ×1
controller ×1
magento ×1
mysql ×1
nativequery ×1