当我运行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;
}
}
答案 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)");
}
}
}