在AJAX调用中尝试在twig文件中打印它们时,关系变量为null

时间:2016-04-07 13:38:42

标签: php ajax wordpress symfony herbert

我正在使用herbert处理wordpress插件。我有两个控制器,一个(ClientController)通过面板调用,第二个(TimelineController)是一个API控制器,用于在时间线上保存新数据,并通过使用路由的AJAX请求调用。

这是我的两个控制器

ClientController

public function edit(Http $request)
{
    $client = $this->clientsRep->find($input["id"]);
    $timeline = $client->timeline;
    return view('@AdminViews/edit.twig',compact('client', 'timeline'));
}

TimelineController

public function store(Http $request)
    {
        if($request->ajax())
        {
            if (is_user_logged_in())
            {
                $class = ucfirst($request->get('type'));
                $c = 'Testing\\HelpedClasses\\TimelineLib\\'.$class; 
                $activity = new $c();
                $activity = $activity->saveActivity($request);
                if (is_array($activity))
                {
                     $type = $activity["type"];
                     $activity = Activities::find($activity["activity_id"]);
                     $returnHTML =  herbert('twig')->render('@AdminViews/edit/timeline_partials/timeline-row.twig', [
                            'activity' => $activity,
                     ]);
                     return new JsonResponse(['success' => true, 'html' => $returnHTML, 'type' => $request->get('type'));
                }
            }
        }
    }

以下是具有关系的模型

class Client extends Model
{
    public function timeline()
    {
        return $this->hasOne('Test\Models\Admin\Timeline', 'timeline_client_id', 'ID');
    }  
}

class Activities extends Model
{
    public function clientNote()
    {
        return $this->hasOne('Test\Models\Admin\Notes', 'notes_timeline_activities_id', 'ID');
    }   

}

以下是观点

edit.twing

<div class="wrap">
    <h1>Edit: {{client.clientMeta.name.value }} {{client.clientMeta.name.lastname }}</h1>
    <div class="postbox ">
        <ul  class="nav nav-pills">
            <li class="active"><a  href="#1a" data-toggle="tab">Account</a></li>
            <li><a href="#timeline" data-toggle="tab">Activity</a></li>
        </ul>
        <div class="inside">
            <div class="tab-content clearfix">
                <div class="tab-pane active" id="1a">
                        {% include '@AdminViews/edit/edit.twig' %}
                </div>
                <div class="tab-pane" id="timeline">
                        {% include '@AdminViews/edit/timeline.twig' %}
                </div>
            </div>
        </div>
    </div>
</div>

timeline.twig

<h3>Activity</h3>
<div class="timeline row">
    <div class="col-sm-12 col-md-4 col-md-push-8">
        <a href="#" data-type="note" class="btn btn-float note-btn">Add Note <i class="fa fa-sticky-note"></i></a>
    </div>
    <div class="col-sm-12 col-md-8 col-md-pull-4 timeline-section">

    {% if timeline is not null %}
    <ul class="timeline">
    {% for activity in timeline %}
    {% include '@AdminViews/edit/timeline_partials/timeline-row.twig' %}
    {% else %}
    <li class="empty-list">Looks like you have no addresses added.</li>
    {% endfor %}
    </ul>
    {% else %}
     <p class="empty-list">Looks like you don't have any timeline yet for this customer.</p>
    {% endif %}

    </div>
</div>

时间轴的row.twig

<li>
 <div class="direction-l"><span class="time-wrapper"><span class="time">{{activity.updated_at}}</span></span></div>
  <div class="direction-r">
   <div class="flag-wrapper">
    <span class="flag">
    {% include '@AdminViews/edit/timeline_partials/timeline-row-'~ activity.timeline_meta_key ~ '.twig' %}
    </span>
  </div>
 </div>
</li>

以下是activity.timeline_meta_key值的示例:timeline-row-note.twig

<span class="timeline-key"> <i class="fa fa-bell-o"></i>{{activity.timeline_meta_key}}</span>
<div class="clearfix"></div>
{% if activity.clientNote %}
<p class="note-subject">{{activity.clientNote.notes_subject}}</p>
{% endif %}
{% if activity.clientNote %}
<p class="note-desc">{{activity.clientNote.notes_description}}</p>
{% endif %}

现在,如果我调用ClientController,它会很好地显示值{{activity.clientNote.notes_subject}}和{{activity.clientNote.notes_description}} 如果现在我调用TimelineController不打印我已经提到的变量,我在twig文件中做了一个dd(),它说变量是null,但是如果我之前做了dd()

$returnHTML =  herbert('twig')->render('@AdminViews/edit/timeline_partials/timeline-row.twig', [
 'activity' => $activity,
]);

之后的活动:: find($ activity [“activity_id”]);这两个变量具有正确的值。有谁知道为什么我没有在twig文件中显示关系activity.clientNote的值,如果我进行ajax调用?

谢谢

0 个答案:

没有答案