改进多态关联的morphTo方法 如果不传入参数默认用当前定义的方法名作为字段前缀

This commit is contained in:
thinkphp
2016-12-01 11:48:01 +08:00
parent 77d2731892
commit 1afdde40e3

View File

@@ -1392,8 +1392,12 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
* @param array $alias 多态别名定义
* @return Relation
*/
public function morphTo($morph, $alias = [])
public function morphTo($morph = null, $alias = [])
{
if (is_null($morph)) {
$trace = debug_backtrace(false, 2);
$morph = Loader::parseName($trace[1]['function']);
}
// 记录当前关联信息
if (is_array($morph)) {
list($foreignKey, $morphType) = $morph;