我将用户角色存储到数据库时遇到问题.我使用的是原始文件:http://symfony.com/doc/current/book/security.html
在这里:http://symfony.com/doc/current/cookbook/security/entity_provider.html
登录注销正在运行.我可以将用户保存到数据库中,但我不知道如何保存他的角色.是否有可能使用这些文件,或者我还需要再做一个单独服务的文件?我不想使用FOSUser Bundle ...谢谢你的回答.这是链接:
AccountController http://pastebin.com/ycrLyUcg
注册 http://pastebin.com/Q7se6pnF
RegistrationType http://pastebin.com/926tu0hj
用户 http://pastebin.com/aNdQr46C 角色 http://pastebin.com/cg7QTk59 UserRepository http://pastebin.com/LHPuy4We
非常感谢您的回答
编辑10.11.2013 23:00:
编辑11.11.2013 15:20
我正在研究一个symfony项目,我尝试使用实体向数据库添加数据,但是我得到了一个Doctrine sql错误?!!!! 所以任何帮助都是先进的
我创建了一个实体myselft,它是:
/**
* @ORM\Table(name="u001_user_group")
* @ORM\Entity
*/
class UserGroup
{
/**
* @var integer
*
* @ORM\Column(name="group", type="integer", length=20, nullable=false)
*/
private $group;
public function setGroup($group)
{
$this->group = $group;
return $this;
}
public function getGroup()
{
return $this->group;
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试添加这样的东西时:
$UserGroup= new UserGroup();
$UserGroup->setUser($registerAction->getId());
$UserGroup->setGroup(4);
$em->persist($UserGroup);
$em->flush();
Run Code Online (Sandbox Code Playgroud)
它给了我这个错误:
使用params [20,4]执行'INSERT INTO u001_user_group(user,group)VALUES(?,?)'时发生异常:
SQLSTATE [42000]:语法错误或访问冲突:1064 SQL语法中有错误; 检查与MySQL服务器版本对应的手册,以便在第1行的'group)VALUES(20,4)附近使用正确的语法
我开始学习sf2,非常酷,因为我的问题我有两个表:
媒体
/**
* @ORM\ManyToMany(targetEntity="Test\SiteBundle\Entity\Website", inversedBy="medias")
* @ORM\JoinTable(name="media_website")
private $websites;
Run Code Online (Sandbox Code Playgroud)
和网站
/**
* @ORM\ManyToMany(targetEntity="Test\SiteBundle\Entity\Media", mappedBy="websites")
private $medias;
Run Code Online (Sandbox Code Playgroud)
在我的MediaType.php中我有这个:
$builder
->add('title')
->add('website', 'entity', array(
'class' => 'TestSiteBundle:Website',
'property' => 'name',
'query_builder' => function(WebsiteRepository $er)use($user_id) {
return $er->getMyWebsites($user_id);
},
'multiple'=>false))
Run Code Online (Sandbox Code Playgroud)
最后,在枝条页面中我有这个:
<div class="form-group">
{{ form_label(form.description, "Description", { 'label_attr': {'class': 'control-label col-md-2'} }) }}
<div class="col-md-5">
{{ form_widget(form.description, { 'attr': {'class': 'form-control'} }) }}
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
当我尝试添加媒体时出现此错误:
Neither the property "websites" nor one of the methods "setWebsites()", "__set()" or "__call()" exist and …
Run Code Online (Sandbox Code Playgroud) Objective
有一个 Perspective
class Objective{
...
public function setPerspective(\Cboujon\BSCBundle\Entity\Pespective $perspective = null)
{
$this->perspective = $perspective;
return $this;
}
}
Run Code Online (Sandbox Code Playgroud)
Objective.orm.yml
Cboujon\BSCBundle\Entity\Objective:
manyToOne:
perspective:
targetEntity: Perspective
inversedBy: objectives
joinColumn:
name: perspective_id
referencesColumn: id
Run Code Online (Sandbox Code Playgroud)
ObjectiveType.php
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('perspective');
}
Run Code Online (Sandbox Code Playgroud)
Symfony2使用具有所有视角的组合框呈现一个表单.那没问题!但当我提交时,create form
我收到错误:
ContextErrorException:Catchable Fatal Error:传递给Cboujon\BSCBundle\Entity \的参数1:Objective :: setPerspective()必须是Cboujon\BSCBundle\Entity\Pespective的实例,Cboujon\BSCBundle\Entity\Perspective的实例,在/ home中调用第376行的/cristhian/php_apps/Symfony/vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php,在/home/cristhian/php_apps/Symfony/src/Cboujon/BSCBundle/Entity/Objective.php中定义第63行
注意:我也尝试:
$builder->add('perspective', 'entity', array(
'class' => 'CboujonBSCBundle:Perspective',
)
)
;
Run Code Online (Sandbox Code Playgroud)
但我得到了同样的错误.
注意2:如果我\Cboujon\BSCBundle\Entity\Pespective
从setPerspective定义中删除,我可以提交表单确定.
我究竟做错了什么?
我有两个与关系连接的实体(AdminMembers\Entity\Members
和AdminEvents\Entity\Invitees
)OneToMany
.因为关系是在实体中定义的,所以我可以使用以下DQL语句来查询数据:
$dql = "SELECT i FROM AdminEvents\Entity\Invitees i WHERE i.eventID=$eventID";
Run Code Online (Sandbox Code Playgroud)
并且,通过这种配置,我可以使用像$invitee->getMember()->getMemberLastName()
我的ZF2视图脚本中的语句来从连接的实体中获取数据.
现在,如果我想按字段对DQL语句中的数据进行排序AdminMembers\Entity\Members
,我遇到了问题.以下声明
$dql = "SELECT i FROM AdminEvents\Entity\Invitees i WHERE i.eventID=$eventID ORDER BY i.memberLastName, i.memberFirstName";
Run Code Online (Sandbox Code Playgroud)
抛出错误消息
Class AdminEvents\Entity\Invitees has no field or association named memberLastName
Run Code Online (Sandbox Code Playgroud)
这是因为字段memberLastName
和memberFirstName
定义AdminMembers\Entity\Members
.虽然关联确实存在,但我确定我只是缺少引用memberLastName
和语法中的一些语法memberFirstName
.
我知道我可以扩展DQL语句以加入DQL语句中的实体,这将允许我识别连接表并将元素与表标识符相关联.但是,由于表已经在实体定义中加入,因此不必再次连接它们.
是否有一种特殊的语法用于在DQL语句中引用连接表实体而不"重新加入"语句中的表?
为什么它不起作用?我怎么称呼存储过程?
$rsm = new \Doctrine\ORM\Query\ResultSetMappingBuilder($this->objectManager);
$query = $this->objectManager->createNativeQuery('call `ftemplate_setcell`(:t, :col, :row, :p, :r, :v)', $rsm)->
setParameters($this->data);
$query->Execute();
$this->objectManager->flush();
Run Code Online (Sandbox Code Playgroud) 当我运行doctrine架构更新时,我得到了这个需要执行的奇怪查询,但它们基本上只是重做已经完成或没有?
php app/console doctrine:schema:update --dump-sql
DROP INDEX idx_26d7e8feab772a3c ON notify;
CREATE INDEX IDX_217BEDC8AB772A3C ON notify (notifyUser_id);
DROP INDEX idx_26d7e8fea76ed395 ON notify;
CREATE INDEX IDX_217BEDC8A76ED395 ON notify (user_id);
DROP INDEX idx_26d7e8fe6bf700bd ON notify;
CREATE INDEX IDX_217BEDC86BF700BD ON notify (status_id);
"require": {
"php": ">=5.3.3",
"symfony/symfony": "~2.5.3",
"doctrine/orm": "~2.2,>=2.2.3",
"doctrine/doctrine-bundle": "~1.2",
"gedmo/doctrine-extensions": "2.3.*@dev",
"stof/doctrine-extensions-bundle": "~1.1@dev",
},
Run Code Online (Sandbox Code Playgroud)
此行为阻止我使用--force更新doctrine架构,因为有外键,我收到此错误:
[PDOException]
SQLSTATE[HY000]: General error: 1553 Cannot drop index 'IDX_A37CA197A76ED395': neede
d in a foreign key constraint
Run Code Online (Sandbox Code Playgroud)
这可能是在过去更新数据库相关供应商之后开始发生的,但直到现在我还没有注意到.
我创建了一个自定义验证器来检查两个数据库表中是否存在电子邮件.
namespace AgriHealth\AhpBundle\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
/**
* @Annotation
*/
class UniqueEmailAll extends Constraint
{
public $message = 'This email is already in use';
public function validatedBy()
{
return get_class($this).'Validator';
}
}
namespace AgriHealth\AhpBundle\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
class UniqueEmailAllValidator extends ConstraintValidator
{
private $entityManager;
public function __construct(EntityManager $entityManager)
{
$this->entityManager = $entityManager;
}
public function validate($value, Constraint $constraint)
{
$find1 = $this->entityManager->getRepository( 'AgriHealthAhpBundle:Vet')->findOneByEmail($value);
$find2 = $this->entityManager->getRepository( 'AgriHealthAhpBundle:Farmer')->findOneByEmail($value);
if ($find1 || $find2) {
// If you're using the new 2.5 validation …
Run Code Online (Sandbox Code Playgroud) 当我尝试持久化我的对象并刷新它时,我收到以下错误消息:
警告:spl_object_hash()期望参数1为对象,给定500内部服务器错误的整数 - ContextErrorException
我知道这种问题已经在堆栈溢出中发布了很多,但它仍然无法解决我的问题.这就是为什么我在这里再问一次,希望有人可以帮助我.
以下是我保留用户类的代码:
$package = $em->getRepository('MyBundle:Package')->findOneBy(array('id' => 1));
$new_user->addPackage($package);
$role = $em->getRepository('MyBundle:Role')->findOneBy(array('id' => 3));
$new_user->addRole($role);
$new_user->setCmsPrize(2); //int
$new_user->setCmsBet(3); //int
$new_user->setName("new user");
$new_user->setUserName("test123");
$new_user->setPassword("abc");
$new_user->setCreditLimit(1000);
$new_user->setCreditBalance(2000);
$new_user->setSelectedPackage($currentuser->getSelectedPackage());
$new_user->setParentId($currentuser->getId());
$new_user->setLayer($currentuser->getLayer() + 1);
$em->persist($new_user);
$em->flush();
Run Code Online (Sandbox Code Playgroud)
以下是我的用户类:
/**
* MyBundle\Entity\User
*
* @ORM\Table(name="")
* @ORM\Entity(repositoryClass="MyBundle\Entity\UserRepository")
*/
class User implements AdvancedUserInterface, \Serializable {
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="string", length=25, unique=true)
*/
private $username;
/**
* @ORM\Column(type="string", length=64, options={"fixed" = true}))
*/
private $password;
/** …
Run Code Online (Sandbox Code Playgroud) 我已经阅读了有关此问题的其他问题,但尚未找到解决方案.
我收到以下错误消息:
在链配置>命名空间ZfcUser\Entity,Common\Entity,Employment\Entity,Intern\Entity,> Team\Entity,PurchaseRequest\Entity中找不到类'Doctrine\ORM\EntityManager'.
我有一个HolidayEntity,HolidayController,HolidayService.
添加假期有效,但当我尝试删除假期时,会弹出错误.我将假日id从控制器传递给服务,然后服务取出相关对象并运行doctrine 2 removeEntity命令.
我不确定如何解决这个问题.
控制器代码:
public function deleteAction()
{
$holidayId = $this->params()->fromRoute('id', 0);
try {
$this->getServiceLocator()->get('holidayService')->removeHoliday($holidayId);
} catch (Exception $e) {
$this->flashMessenger()->addErrorMessage($e->getMessage() . '<br>' . $e->getTraceAsString());
}
return $this->redirect()->toRoute('holiday/list');
}
Run Code Online (Sandbox Code Playgroud)
服务代码:
public function removeHoliday($holidayId)
{
try{
$holiday = $this->findOneHolidayById($holidayId);
$this->removeEntity($holiday);
} catch (Exception $e) {
var_dump($e);
}
}
protected function removeEntity($entity)
{
$this->getEntityManager()->remove($entity);
$this->getEntityManager()->flush();
}
Run Code Online (Sandbox Code Playgroud)
代码在"$ this-> getEntityManager() - > remove($ entity)"方法中断.
doctrine-orm ×10
symfony ×7
php ×5
doctrine ×3
dql ×1
forms ×1
many-to-many ×1
mysql ×1
symfony-2.5 ×1
validation ×1