# |
Apr 23rd 2021, 19:42 |
slackebot |
'/my-controller-name/ajax-get-offers/' + 1234, headers : { 'X-CSRF-Token': $('[name="_csrfToken"]').val() }, success: function(data, textStatus, jqXHR){ console.log(data) }, });``` As you can see you need a valid `_csrfToken` which you will get from any valid `<form>` created by the FormHelper in your cakephp template |
# |
Apr 23rd 2021, 19:42 |
slackebot |
/my-controller-name/ajax-get-offers/1234 then $id will be 1234 $data = ["test" => "hello!"]; $this->autoRender = false; return $response->withType( 'application/json' ) ->withStringBody( json_encode( [ 'data' => $data ] ) ); } }``` then call that function via the usual cakephp naming conventions like that in your ajax call ``` $.ajax({ type: 'get', url: |
# |
Apr 23rd 2021, 19:42 |
kevin.pfeifer |
the “short” gist of it is the following: create a controller function which you want to call via ajax ``` public function ajaxGetOffers( $id = null ): Response { $request = $this->getRequest(); $response = $this->getResponse(); if( $request->is( 'ajax' ) andand $request->is( 'get' ) ) { // TODO: Do your loading of your data here. // If this function is called via |
# |
Apr 23rd 2021, 19:23 |
sebastiansperandio093 |
Hello everyone, Hello everyone, could someone help me about how is the best cakephp's way to make an ajax request from a view that contain a form? I need to populate a dynamic select element in the form. I just need a documentation link or whatever....thanks! |
# |
Apr 23rd 2021, 18:05 |
tyler.adam.lazenby |
:) |
# |
Apr 23rd 2021, 18:05 |
kevin.pfeifer |
well, probably something for quick prototyping |
# |
Apr 23rd 2021, 18:05 |
kevin.pfeifer |
i see |
# |
Apr 23rd 2021, 18:05 |
tyler.adam.lazenby |
it was suggested in the entity documentation |
# |
Apr 23rd 2021, 18:04 |
tyler.adam.lazenby |
:) |
# |
Apr 23rd 2021, 18:04 |
tyler.adam.lazenby |
yes |
# |
Apr 23rd 2021, 17:39 |
kevin.pfeifer |
you mean this, right? https://github.com/jeremyharris/cakephp-lazyload |
# |
Apr 23rd 2021, 17:38 |
tyler.adam.lazenby |
its nice |
# |
Apr 23rd 2021, 17:38 |
kevin.pfeifer |
but i will have to take a look at that lazyload trait, it seems pretty cool :thinking_face: |
# |
Apr 23rd 2021, 17:36 |
kevin.pfeifer |
the "default", as little code as possible, cakephp way is just preconfigured with quite easy to understand defaults |
# |
Apr 23rd 2021, 17:36 |
tyler.adam.lazenby |
Yup, thanks for your help |
# |
Apr 23rd 2021, 17:35 |
kevin.pfeifer |
you can basically create any assiciation as you like |
# |
Apr 23rd 2021, 17:35 |
kevin.pfeifer |
so as you see |
# |
Apr 23rd 2021, 17:35 |
kevin.pfeifer |
:) |
# |
Apr 23rd 2021, 17:35 |
tyler.adam.lazenby |
That .... actually makes sense |
# |
Apr 23rd 2021, 17:34 |
kevin.pfeifer |
since you used `'propertyName' => 'User',` |
# |
Apr 23rd 2021, 17:34 |
tyler.adam.lazenby |
smh its always something stupidly simple like that |
# |
Apr 23rd 2021, 17:33 |
kevin.pfeifer |
could it be that its actually `$account->User` ? |
# |
Apr 23rd 2021, 17:32 |
slackebot |
LIMIT 1``` |
# |
Apr 23rd 2021, 17:32 |
slackebot |
User__last_name, User.phone AS User__phone, User.email AS User__email, User.login AS User__login, User.password AS User__password, User.created AS User__created, User.updated AS User__updated, User.company_name AS User__company_name, User.token AS User__token, User.account_id AS User__account_id FROM accounts Accounts INNER JOIN users User ON User.id = (Accounts.user_id) WHERE Accounts.id = 15 |
# |
Apr 23rd 2021, 17:32 |
tyler.adam.lazenby |
```SELECT Accounts.id AS Accounts__id, Accounts.user_id AS Accounts__user_id, Accounts.name AS Accounts__name, Accounts.logo AS Accounts__logo, Accounts.created AS Accounts__created, Accounts.modified AS Accounts__modified, Accounts.phone AS Accounts__phone, User.id AS User__id, User.user_type_id AS User__user_type_id, User.first_name AS User__first_name, User.last_name AS |
# |
Apr 23rd 2021, 17:30 |
tyler.adam.lazenby |
let me get rid of the dd and I will check |
# |
Apr 23rd 2021, 17:30 |
kevin.pfeifer |
or is there something wrong with the join |
# |
Apr 23rd 2021, 17:30 |
kevin.pfeifer |
sql looks right in debug kit? |
# |
Apr 23rd 2021, 17:29 |
tyler.adam.lazenby |
but just for kicks and giggles I just tried it, same result |
# |
Apr 23rd 2021, 17:29 |
kevin.pfeifer |
ok i have no experience with that ,:) |
# |
Apr 23rd 2021, 17:28 |
tyler.adam.lazenby |
I am not using the contain command because I am using the lazyloadtrait |
# |
Apr 23rd 2021, 17:28 |
kevin.pfeifer |
in your query |
# |
Apr 23rd 2021, 17:28 |
kevin.pfeifer |
do you contain `User` as well now? |
# |
Apr 23rd 2021, 17:27 |
tyler.adam.lazenby |
I have made sure to have the lazyloadingtrait being used by my account entity, but when I call $account->user.... it returns null |
# |
Apr 23rd 2021, 17:26 |
tyler.adam.lazenby |
hmmm |
# |
Apr 23rd 2021, 17:26 |
kevin.pfeifer |
one `User` and one `Users` yes |
# |
Apr 23rd 2021, 17:25 |
tyler.adam.lazenby |
So this isn't going to create two properties (users and user)? ```$this->belongsTo('User', [ 'foreignKey' => 'user_id', 'joinType' => 'INNER', 'className' => 'Users', 'bindingKey' => 'id', 'propertyName' => 'User', ]); $this->hasMany('Users', [ 'foreignKey' => 'account_id' ]);``` |
# |
Apr 23rd 2021, 17:24 |
kevin.pfeifer |
but you can basically set the first parameter (for me `ParentProjects` and `ChildProjects` ) as whatever you like |
# |
Apr 23rd 2021, 17:24 |
blackjccl |
Thank you that was the mistake @kevin.pfeifer @admad |
# |
Apr 23rd 2021, 17:24 |
kevin.pfeifer |
depending on your associations you need to set the right parameters described in https://book.cakephp.org/4/en/orm/associations.html |
# |
Apr 23rd 2021, 17:23 |
slackebot |
projects i call `$project = $this->Projects->get( $id, [ 'contain' => ['Childcts'] ]);` |