0

関連するデータオブジェクトの属性をテキストフィールドから直接編集しようとしています。たとえば、すべてのページに関連するカウンター オブジェクトがあります。関連オブジェクトに移動してそこで編集するのではなく、テキストフィールドを介してカウンターオブジェクトの値を直接編集すると非常に便利です。それは可能ですか?

少なくとも、次のようにテキストフィールドに現在の値を表示できます。$fields->addFieldToTab('Root.Main', new TextField('ILikeCount.Count', 'ILikeCount', $this->ILikeCount()->Count), 'Content');

ただし、新しい値を保存しても機能しません。どうもありがとう、フロリアン

4

1 に答える 1

0

onBeforeWrite()たぶん、フックで textField 値をキャッチできます

$fields->addFieldToTab('Root.Main', new TextField('ILikeCountCount', 'ILikeCount', $this->ILikeCount()->Count), 'Content');

textField の名前は何でもかまいません。ここでは、わかりやすくするためにドット表記を削除しました。次に、値をキャッチしonBeforeWrite()てリレーションを更新します。

public function onBeforeWrite()
{
    if( $this->ILikeCountCount )
    {
        // check if a $has_on realtion exist
        if ( !$this->ILikeCountID )
        {
            // create new DataObject + relation
            $ILikeCount = ILikeCount::create();
            $ILikeCount->Count = $this->ILikeCountCount;
            $ILikeCount->write();

            $this->ILikeCountID = $ILikeCount->ID;
        }
        else{
            // update existing relation
            $ILikeCount = $this->ILikeCount();
            $ILikeCount->Count = $this->ILikeCountCount;
            $ILikeCount->write();
        }  

        $this->ILikeCountCount = false; // clear to avoid duplicate writes
    }
    parent::onBeforeWrite();
}
于 2013-10-29T07:41:24.390 に答える