Cakephp select from where jquery null

谁说我不能喝 提交于 2019-12-23 05:04:07

问题


Here are my Code:

<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" id="animal"  value="1">Animal1</a>
<a class="dropdown-item" id="animal"  value="2">Animal2</a>
<a class="dropdown-item" id="animal"  value="3">Animal3</a>
</div>

I have an jQuery one click:

$(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
            }
        });
    })
});

Now I have my controller - here I "should" get the value:

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

So let's see the SQL statement:

$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
  )

So that is my problem. After click he must to turn me the value of animal soo that i can make an select from. And only if I use :

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

it returns the name from the SQL statement. Why? I need this value of animal to use it also for intern in controller

INSERT INTO WHERE animalid = $jqueryanimalvalue

回答1:


Your SQL Query is wrong:

Change this:

$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
]);

To:

$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'
        ]
    ]
]);


来源:https://stackoverflow.com/questions/48336752/cakephp-select-from-where-jquery-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!