Php 在输入时拆分字段,而不是在输出时拆分字段
我目前正在使用这个函数Php 在输入时拆分字段,而不是在输出时拆分字段,php,yii,Php,Yii,我目前正在使用这个函数 public $year; public $month; public $day; protected function afterFind() { parent::afterFind(); $dob = explode('/', $this->dob); $this->year = $dob[0]; $this->month = $dob[1]; $this->day = $dob[2]; r
public $year;
public $month;
public $day;
protected function afterFind() {
parent::afterFind();
$dob = explode('/', $this->dob);
$this->year = $dob[0];
$this->month = $dob[1];
$this->day = $dob[2];
return $this;
}
protected function beforeSave() {
$this->dob = $this->year .'/'. $this->month .'/'. $this->day;
}
<div class="col-md-2 dayForm noPadding">
<?php
echo $form->textFieldGroup(
$user,
'day',
array(
'widgetOptions' => array(
'htmlOptions' => array(
'placeholder' => 'DD'
)
)
)
);
?>
</div>
<div class="col-md-3 monthForm ">
<?php
echo $form->dropDownListGroup(
$user,
'month',
array(
'widgetOptions' => array(
'data' => array('01' => 'January' , '02' => 'February' , '03' => 'March' , '04' => 'April' , '05' => 'May' , '06' => 'June' , '07' =>'July' , '08' =>'August' , '09' =>'September' , '10' =>'October' , '11' =>'November' , '12' =>'December'),
// 'data' => 'Jan','Feb';
'htmlOptions' => array(
'class' => 'col-md-3 ',
'prompt' => 'Choose month',
),
)
)
);
?>
</div>
<div class="col-md-2 yearForm noPadding">
<?php
echo $form->textFieldGroup(
$user,
'year',
array(
'widgetOptions' => array(
'htmlOptions' => array(
'placeholder' => 'YYYY',
'class' => 'col-md-3',
)
)
)
);
?>
</div>
public$year;
公众每月$;
公费日;;
受保护函数afterFind(){
父::afterFind();
$dob=爆炸('/',$this->dob);
$this->year=$dob[0];
$this->month=$dob[1];
$this->day=$dob[2];
退还$this;
}
保存()之前的受保护函数{
$this->dob=$this->year.'/'.$this->month.'/'.$this->day;
}
将出生日期字段拆分为3个单独的字段。简单正确,用户输入日期、月份和年份,并以正确的格式显示。我遇到的问题是,当用户去更新整个dob字段时,所有字段都显示在年度textfieldgroup中,这不太好
如何在退出时将其重新分解,以便所有相应字段都显示在其textfieldgroups/dropdownlist组中?我总是尽量避免使用
afterFind
,因为这会使事情变得复杂,难以解决像您这样的问题。您通常可以通过添加getter(和可选的setter)来获得类似的结果,如下所示
Yii将负责将这些getter/setter作为属性自动提供(例如$class->year)
您的视图代码应该保持不变
另外,通常最好的做法是在重写父方法时返回它们的结果。例如,
beforeSave()
的最后一行通常应该是returnparent::beforeSave()代码>。如果您没有这样做,您的类甚至可能无法正确保存。removereturn$this
在after-find中还将parent::afterFind()
放在函数的最后一行,所以完全删除并返回$this,并在末尾添加parent::afterFind()?尝试此操作,但未进行任何更改。有什么想法吗?你能发布控制器代码吗?我应该在控制器中寻找什么?我有actionCreate、actionView和ActionUpdate您遇到问题的操作,即更新我认为逻辑看起来不错,但我在这一行遇到错误,公共函数getYear(){return$this->getDobParts()[0];}看起来代码示例以两个setMonth
s结尾,将第二个重命名为setDay
。如果这仍然不起作用,那么您介意告诉我您的PHP版本吗?(php-v
)是的,我修复了我看到的,php版本是5.6.3,它仍然默认dob字段为最后一个字段“year”。你确定你的日期格式是yyyy/mm/dd而不是yyyy-mm-dd吗?我应该提到,在函数中使用或/时使用它没有区别,它仍然可以很好地输入,但不管我使用什么,它要么不返回文本字段,要么在年份字段中填写。
protected function getDobParts()
{
return explode('/', $this->dob);
}
protected function changeDobPart($part, $newValue)
{
$parts = $this->getDobParts();
$parts[$part] = $newValue;
$this->dob = implode('/', $parts);
}
public function getYear()
{
return $this->getDobParts()[0];
}
public function setYear($value)
{
$this->changeDobPart(0, $value);
}
public function getMonth()
{
return $this->getDobParts()[1];
}
public function setMonth($value)
{
$this->changeDobPart(1, $value);
}
public function getDay()
{
return $this->getDobParts()[2];
}
public function setMonth($value)
{
$this->changeDobPart(2, $value);
}