查看:
#set ($array = $TYPE_HINT.split('\|'));
#set ($IS_COLLECTION = false);
#set ($FIXED_RETURN_TYPE = $RETURN_TYPE);
#set ($NAME_LENGTH = $FIELD_NAME.length());
#set ($NAME_BEFORE_LAST = $FIELD_NAME.length() - 1);
#set ($LAST_CHARACTER = $FIELD_NAME.substring($NAME_BEFORE_LAST, $NAME_LENGTH));
#set ($METHOD_NAME_BEFORE_LAST = $NAME.length() - 1);
#if (${LAST_CHARACTER} == 's')
#set ($FIXED_PROPERTY_NAME = $FIELD_NAME.substring(0, $NAME_BEFORE_LAST));
#set ($FIXED_METHOD_NAMING_PART = $NAME.substring(0, $METHOD_NAME_BEFORE_LAST));
#else
#set ($FIXED_PROPERTY_NAME = $FIELD_NAME);
#set ($FIXED_METHOD_NAMING_PART = $NAME);
#end
#foreach($hint in $array)
#if ($hint.toString().contains('[]'))
#set ($ENTRY_HINT = $hint.toString().replace('[]', ''));
#set ($HAS_ENTRY_HINT = true);
#end
#end
#foreach($hint in $array)
#if ($hint == 'Collection')
#set ($IS_COLLECTION = true)
#set ($FIXED_RETURN_TYPE = 'Collection')
#break
#elseif ($hint == 'ArrayCollection')
#set ($IS_COLLECTION = true)
#set ($FIXED_RETURN_TYPE = 'Collection')
#break
#elseif ($hint == 'array')
#set ($IS_COLLECTION = true)
#set ($FIXED_RETURN_TYPE = 'array')
#elseif ($hint.toString().contains('[]'))
#set ($IS_COLLECTION = true)
#set ($FIXED_RETURN_TYPE = 'array')
#end
#end
#if ($IS_COLLECTION)
/**
* @param ${ENTRY_HINT} $${FIXED_PROPERTY_NAME}
*
* @return ${CLASS_NAME}
*/
public function add${FIXED_METHOD_NAMING_PART}(#if (${HAS_ENTRY_HINT})${ENTRY_HINT} #else#end$${FIXED_PROPERTY_NAME})#if(${RETURN_TYPE}): ${CLASS_NAME}#else#end
{
#if ($FIXED_RETURN_TYPE == 'Collection')
if(false === $this->${FIELD_NAME}->contains($${FIXED_PROPERTY_NAME})) {
$this->${FIELD_NAME}->add($${FIXED_PROPERTY_NAME});
}
#else
if(false === in_array($${FIXED_PROPERTY_NAME}, $this->${FIELD_NAME}, true)) {
$this->${FIELD_NAME}[] = $${FIXED_PROPERTY_NAME};
}
#end
return $this;
}
/**
* @param ${ENTRY_HINT} $${FIXED_PROPERTY_NAME}
*
* @return ${CLASS_NAME}
*/
public function remove${FIXED_METHOD_NAMING_PART}(#if (${HAS_ENTRY_HINT})${ENTRY_HINT} #else#end$${FIXED_PROPERTY_NAME})#if(${RETURN_TYPE}): ${CLASS_NAME}#else#end
{
#if ($FIXED_RETURN_TYPE == 'Collection')
if(true === $this->${FIELD_NAME}->contains($${FIXED_PROPERTY_NAME})) {
$this->${FIELD_NAME}->removeElement($${FIXED_PROPERTY_NAME});
}
#else
if(true === in_array($${FIXED_PROPERTY_NAME}, $this->${FIELD_NAME}, true)) {
$index = array_search($${FIXED_PROPERTY_NAME}, $this->${FIELD_NAME});
array_splice($this->${FIELD_NAME}, $index, 1);
}
#end
return $this;
}
#end
/**
* @param ${TYPE_HINT} $${PARAM_NAME}
* @return ${CLASS_NAME}
*/
public function set${NAME}(#if (${SCALAR_TYPE_HINT})${SCALAR_TYPE_HINT} #else#end$${PARAM_NAME})#if(${RETURN_TYPE}): ${CLASS_NAME}#else#end
{
$this->${FIELD_NAME} = $${PARAM_NAME};
return $this;
}
我已經設法創建了類似的東西,隨時修改您的需求。這是流利的設置器活動模板。
它適用於Doctrine的Collection/ArrayCollection數組。擴展此功能與創建新的#elseif語句一樣簡單。
/**
* @var array|string[]
*/
protected $oneField;
/**
* @var ArrayCollection|Collection|Another[]
*
* @ORM\OneToMany(targetEntity="Acme\DemoBundle\Entity\Another")
*/
protected $anotherField;
謝謝你。我正在嘗試生活的tempaltes。我爲PHP代碼添加了一個,但它不起作用。我需要以某種方式激活它嗎? – HappyCoder
截圖請描述你做了什麼(它不工作)。如果沒有它們,它將只是瘋狂的猜測(例如,根本沒有上下文......或指定了錯誤的上下文;使用了錯誤的擴展鍵;無效/錯誤的活模板體等)。 – LazyOne
ntw - 全部排序,感謝您的幫助!非常感謝並且非常享受已經轉移到PHPstorm。 – HappyCoder