在控制器symfony3中以一对一的关系自动设置关系值

时间:2018-03-12 22:23:22

标签: php symfony doctrine symfony-3.4

一些背景资料。我有两个具有双向关系的实体:实质上,申请表可以只有一个推荐形式。当推荐者填写表格时,我希望自动填写表格,并且只根据他们在我创建的自定义表格中输入的特定代码选择相关的申请表格(保护其他用户的数据)。我不确定它是怎么发生的,但我让它工作了一次,但由于某些未知的原因,它停止了,并在我的数据库中的推荐表单中为申请人表单ID添加“NULL”。我将包含所有可以提供的信息:

RecommendationForm实体:     

add_filter('upload_mimes', function ( $mimes ) {
    $mimes['*'] = 'text/plain';
    return $mimes;
});

add_filter( 'wp_check_filetype_and_ext', function ( $types, $file, $filename, $mimes) {
    # If filename doesn't containg a dot '.', allow upload
    if( false === strpos( $filename, '.' ) ) {
        $types['ext'] = '*';
        $types['type'] = 'text/plain';
    }
    return $types;
}, 10, 4 );

申请人表格实体:     

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * RecommendationForm
 *
 * @ORM\Table(name="recommendation_form")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\RecommendationFormRepository")
 */
class RecommendationForm
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * One RecommendationForm has One ApplicantForm.
     * @ORM\OneToOne(targetEntity="ApplicantForm", inversedBy="recommendation_form")
     * @ORM\JoinColumn(name="applicant_form_id", referencedColumnName="id")
     */
    private $applicant_form;

    //....

    public function __construct($applicant_form) {
        date_default_timezone_set('America/New_York');

        $this->applicant_form = $applicant_form;

        $this->submitDate = new \DateTime("now");
    }


    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }


    // /**
    //  * Set applicantForm
    //  *
    //  * @param integer $applicant_form
    //  *
    //  * @return RecommendationForm
    //  */
    // public function setApplicantForm($applicant_form)
    // {
    //     $this->applicant_form = $applicant_form;

    //     return $this;
    // }

    /**
     * Get applicantForm
     *
     * @return integer
     */
    public function getApplicantForm()
    {
        return $this->applicant_form;
    }

//....
}

RecommendationController:     

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use chriskacerguis\Randomstring\Randomstring;

/**
 * ApplicantForm
 *
 * @ORM\Table(name="applicant_form")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\ApplicantFormRepository")
 */
class ApplicantForm
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * One ApplicantForm has One RecommendationForm.
     * @ORM\OneToOne(targetEntity="RecommendationForm", mappedBy="applicant_form")
     */
    private $recommendation_form;

    /**
     * @var string
     *
     * @ORM\Column(name="accessCode", type="string", length=75, unique=true)
     */
    private $accessCode;

    //....

    public function __construct() {

        $random = new \chriskacerguis\Randomstring\Randomstring();

        $this->accessCode = $random->generate(15, true);
    }

//...
}

我在互联网上搜索过,找不到答案或弄清楚它是如何停止工作的。

1 个答案:

答案 0 :(得分:0)

我知道我改变了什么,我会把答案放在这里以防万一有人想要这样做。我正在使用不同的表单方法,并将表单的原始方法从“GET”更改为POST(并且对控制器中的值的所有调用都从“GET”更改为“POST”)。一时兴起,我把它改回来了,它再次起作用。我很确定我改变它因为我不喜欢get方法将查询字符串放在地址栏中并错误地认为将方法更改为post会修复它。如果有人建议使用什么而不是那个,我欢迎所有的建议。

不工作的控制器:

//...
public function newAction(Request $request)
{
    if(isset($_POST["accessCodeSS"])){

        $this->accessCode = $_POST["accessCodeSS"];
//...
}

正确的工作控制器:

public function newAction(Request $request)
{
    if(isset($_GET["accessCodeSS"])){

        $this->accessCode = $_GET["accessCodeSS"];
//...
}

正确的表格(index.html.twig):

<form action="{{ path('recommendation_form_new') }}" method="GET">
    <input type="email" name="email" placeholder="Email Address">
    <input type="password" name="accessCodeSS" placeholder="Access Code">
    <input type="submit" name="submit" value="Submit">
</form>