【问题标题】:Create an object when saving another保存另一个对象时创建一个对象
【发布时间】:2012-03-07 22:35:29
【问题描述】:

在我的数据库中,我有以下表格:"clients"、"userProfile" 和 "sfGuardUsers"

我希望系统在我创建新客户端时创建配置文件和用户。这是我到目前为止所拥有的:

在 lib/form/doctrine/ClientForm.class.php 中:

class ClientForm extends BaseClientForm
{
  public function configure()
  {
    parent::configure();

    $userProfileForm = new UserProfileForm($this->object->UserProfile->user);
    $this->mergeForm($userProfileForm);
  }
}

在 modules/client/action/actions.class.php 中:

class clientActions extends autoClientActions
{
  public function executeCreate(sfWebRequest $request)
  {
    parent::executeCreate($request);
    $this->client->createProfile($request->getParameter('email'),$request->getParameter('password'));
  }

在 lib/model/doctrine/Client.class.php 中:

  public function createProfile($email, $password)
  {
    $profil = Doctrine_Core::getTable('UserProfile')->findOneByClientId($this->getId());
    $profil->createUser($email, $password);
    $profil->setClient($this);
    $profil->save();
  }

根据日志,createProfile(null, null)被调用,mysql创建了一个用户''作为用户名:(

【问题讨论】:

    标签: symfony1 symfony-1.4 sfguard sfdoctrineguard


    【解决方案1】:

    您不需要这样做。检查自述文件。 你需要这个app.yml:

    sf_guard_plugin:
      profile_class:      sfGuardUserProfile
      profile_field_name: user_id
    

    当然,在您的 db 架构中,在定义配置文件表时,您还需要定义其与 sf_guard_user table 的关系。

    插件将为您完成其余的工作(在需要时添加/保存新配置文件)。

    【讨论】:

    • 问题是,我的用户可以是“客户”或“供应商”,所以我在配置文件类中只有client_id 和supplier_id。所以当我添加客户时,我必须保存它,然后是配置文件,然后是 sfuser
    • 您如何确定它是供应商还是客户?不同的形式?然后你可以覆盖表单 save/doSave/updateObject 方法(取决于你的情况)和类似$this->getObject()->getProfile()->setType('client')
    • 是的,管理员将使用一种形式来创建供应商,另一种形式来创建客户。两者都应该创建个人资料、sfUser,并将所有人联系在一起。
    猜你喜欢
    • 1970-01-01
    • 2011-09-10
    • 1970-01-01
    • 1970-01-01
    • 2015-05-27
    • 2015-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多