Symfony 2 ContextErrorException运行Doctrine迁移时,只应通过引用传递变量

时间:2014-12-17 17:16:52

标签: php symfony dns email-validation mx-record

当我运行Doctrine Migration时,出现以下错误:

  

迁移20141217162533在执行期间失败。错误运行时   注意:只应通过引用传递变量

     

[Symfony \ Component \ Debug \ Exception \ ContextErrorException]运行时   注意:只应通过引用传递变量

$this->validateUsername行上

。当我发表评论时,它运作正常。

这似乎是一个非常奇怪的运行时错误,我无法理解为什么我会得到这个。

public function up(Schema $schema)
{
    foreach ($this->old_users as $old_user) {
        if ($this->validateUsername($old_user['email']) === false or $this->checkDNSRecord($old_user['email']) === false) {
            $this->addSql("INSERT INTO User (email, joined_on, unsubscribed) VALUES ('" . $old_user['email'] . "', '" . $old_user['joined_on'] . "', 0)");
        }
    }
}

/**
 * Validate the username/email address based on the structure (e.g. username@domain.com)
 *
 * @param $email
 *
 * @return bool
 */
public function validateUsername($email)
{
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        // email is NOT valid
        return false;
    } else {
        // email is valid
        return true;
    }
}

/**
 * Validate the MX line of the DNS record for the host of the email address (e.g. @aol.co.uk)
 *
 * @param $email
 *
 * @return bool
 */
public function checkDNSRecord($email)
{
    if (checkdnsrr(array_pop(explode('@', $email)), 'MX')) {
        return true;
    } else {
        return false;
    }
}

2 个答案:

答案 0 :(得分:5)

问题是像array_pop这样的函数通过引用获取数组,因为它改变了它的内容(从数组中删除了最后一个元素)。现在,您将explode的结果直接传递给array_pop array_pop(explode('@', $email))因此,php无法通过引用传递该结果,因为它不是变量。解决方案非常简单,只需将explode结果保存在临时变量中:

<?php
/* ... */
$mailParts = explode('@', $email);
checkdnsrr(array_pop($mailParts), 'MX');
/* ... */

答案 1 :(得分:0)

这会更好吗?

public function up(Schema $schema)
{
    foreach ($this->old_users as $old_user) {
        $old_email = $old_user['email'];
        if ($this->validateUsername($old_email) === false or $this->checkDNSRecord($old_user['email']) === false) {
            $this->addSql("INSERT INTO User (email, joined_on, unsubscribed) VALUES ('" . $old_user['email'] . "', '" . $old_user['joined_on'] . "', 0)");
        }
    }
}