Cakephp从jquery null中选择

这是我的代码:

 

我点击一下jQuery:

 $(document).ready(function() { $("a#animal.dropdown-item").click(function() { var animalvalue = $(this).attr("value"); $.ajax({ type: 'POST', url: "/mycontroller/", data: {animalvalue:animalvalue}, cache: false, success: function(data) { alert(data); //$("#show").html(data);// here is the part of the problem } }); }) }); 

现在我有了我的控制器 – 在这里我“应该”获得价值:

 $jqueryanimalvalue = $this->request->getdata('animalvalue'); 

那么让我们看一下SQL语句:

 $posts = $this->Posts->find() ->select() ->join([ 'a' => [ 'table' => 'animals', 'type' => 'INNER', 'conditions' =>'a.id = Posts.animal_id' ], 'u' => [ 'table' => 'users', 'type' => 'INNER', 'conditions' =>'u.id = a.user_id' ] ]) ->where(['u.id' => '2', 'a.id'=> $jqueryanimalvalue //here is null ]); SELECT * FROM posts Posts INNER JOIN animals a ON a.id = Posts.animal_id INNER JOIN users u ON u.id = a.user_id WHERE ( u.id = '2' AND a.id = NULL //thats what i mean ) 

这就是我的问题。 点击后他必须把动物的价值转向我,以便我可以选择。 只有我使用:

 $("#show").html(data); 

它从SQL语句返回名称。 为什么? 我需要这个动物的价值也可以用于控制器中的实习生

 INSERT INTO WHERE animalid = $jqueryanimalvalue 

您的SQL查询错误:

改变这个:

 $posts = $this->Posts->find() ->select() ->join([ 'a' => [ 'table' => 'animals', 'type' => 'INNER', 'conditions' =>'a.id = Posts.animal_id' ], 'u' => [ 'table' => 'users', 'type' => 'INNER', 'conditions' =>'u.id = a.user_id' ] ]) ->where(['u.id' => '2', 'a.id'=> $jqueryanimalvalue //here is null ]); 

至:

 $posts = $this->Posts->find() ->select() ->join([ 'a' => [ 'table' => 'animals', 'type' => 'INNER', 'conditions' => [ 'a.id = Posts.animal_id', 'a.id IS ' . $jqueryanimalvalue // if it is null ] ], 'u' => [ 'table' => 'users', 'type' => 'INNER', 'conditions' => [ 'u.id = a.user_id', 'u.id' => '2' ] ] ]);