⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

Issue setrelation + callbackColumn



beachballer

beachballer
  • profile picture
  • Member

Posted 20 November 2017 - 10:40 AM

Dear all,

 

I love grocery crud and bought it already.  :D

 

For dont display IDs as numbers in the view i use setrelation (display full name). Works fine.

 

I do like that feature alot but also need to use this specific field as a link in the view field.

 

Question: When i use callbackColumn the fullname relation stops working, How can i display the link and the fullname instead of the ID?

 

Following DB structrure.

 

 

Table Orders

OrderID ID INT

Productname   Varchar

Price   Decimal

CustomerId foreign ID INT

 

Table Customer

CustomerId ID INT

Firstname   Varchar

Lastname   Varchar

 

Below the code. Please help me to get it work.

 

<?php
 
include("vendor/autoload.php");
 
use GroceryCrud\Core\GroceryCrud;
 
$database = include('database.php');
$config = include('config.php');
 
$crud = new GroceryCrud($config, $database);
 
$crud->setTable('Orders');
$crud->columns(['Productname','Price','CustomerId']);
$crud->setrelation('CustomerId','Customer','{Firstname} {Lastname}');
$crud->callbackColumn('CustomerId', function ($value, $row) {
$test = "<a href='/example2/customers.php#/edit/" . $row->CustomerId."' target='frame'>".$row->CustomerId."</a>";
    return $test;
});
$output = $crud->render();
 
if ($output->isJSONResponse) {
    header('Content-Type: application/json; charset=utf-8');
    echo $output->output;
    exit;
}
 
$css_files = $output->css_files;
$js_files = $output->js_files;
$output = $output->output;
 
include('view.php');
 

Thanks for your support.

Best regards

 

Gregor


Amit Shah

Amit Shah
  • profile picture
  • Member

Posted 20 November 2017 - 11:54 AM

Well - will suggest - don't go for both set relation and column callback.

In your case i will recommend - a column callback. Why - because u not only want to show full name but also show it as link. 

You can query for the data in column callback / format it to required pattern and return it.

 

Now you still can use setRelation .. along with column callback. But there is a trick - set the column callback only when the state is list .. and rest all let it be setRelation.

 

Hope this resolves your query

 

Happy GCing :)


beachballer

beachballer
  • profile picture
  • Member

Posted 20 November 2017 - 12:03 PM

Hey,
 
how to query data in column callback from a different table?
 
$crud->setTable('Orders');
$crud->columns(['Productname','Price','CustomerId']);
$crud->callbackColumn('CustomerId', function ($value, $row) {
$test = "<a href='/example2/customers.php#/edit/" . $row->CustomerId."' target='frame'>".$row->CustomerId."</a>";
    return $test;
});
 

Best regards

Gregor


darkstalker

darkstalker
  • profile picture
  • Member

Posted 20 November 2017 - 12:48 PM

Well, just create a function in one of your models that return the array of data you want giving the id.
Then call that function inside the callback column function and you are all set.

Amit Shah

Amit Shah
  • profile picture
  • Member

Posted 20 November 2017 - 16:56 PM

 

Hey,
 
how to query data in column callback from a different table?
 

$crud->setTable('Orders');
$crud->columns(['Productname','Price','CustomerId']);
$crud->callbackColumn('CustomerId', function ($value, $row) {
$test = "<a href='/example2/customers.php#/edit/" . $row->CustomerId."' target='frame'>".$row->CustomerId."</a>";
    return $test;
});
 

Best regards

Gregor

 

We;; DarlStalker is correct .. 

let me take u a bit deeper in within.

 

when u make a callback column - u get 2 params ($value, $row) .. the $value represents the actual value of the field for which the column is called back for.. and the row is the exact row. With the value or some field from row - you surely can make enough data to query the other table, get back with desired output .. and there you have .. the option to get desired output.

 

Happy GCing :)


beachballer

beachballer
  • profile picture
  • Member

Posted 20 November 2017 - 19:05 PM

Dear all,

 

I realy need you help. Can someone please send the answer as code? I tried all i can find on the internet but had no luck. I tried the following code.

 

 

$crud->setTable('Orders');
$crud->columns(['Productname','Price','CustomerId']);
 
$crud->callbackColumn('CustomerId', function ($value, $row) {
$sql = "SELECT t.Firstname FROM Customer t WHERE t.CustomerId = $row->CustomerId";
    $result = $this->db->query($sql)->row();
return $result->Firstname;
});
 

Best regards

 

Gregor


Amit Shah

Amit Shah
  • profile picture
  • Member

Posted 20 November 2017 - 19:13 PM

Well my friend - what is the problem in here? 


beachballer

beachballer
  • profile picture
  • Member

Posted 20 November 2017 - 19:33 PM

Hey,

 

thanks for the fast reply. I get an error message.

 

error.log  PHP Fatal error:  Using $this when not in object context in 

 

Something went wrong!

We can\'t process the response of the server. This is all we know about it:

Best regards

Gregor


Amit Shah

Amit Shah
  • profile picture
  • Member

Posted 21 November 2017 - 04:08 AM

Well.. $this is usable in classes / objects .. and not helper files. You might be trying to use it in some function base helper files - that is the reason why u must be getting this issue.


beachballer

beachballer
  • profile picture
  • Member

Posted 21 November 2017 - 13:43 PM

Hey,

 

sorry. All feedback given so far was not helping me :ph34r: . Can you please share example based on Grocery crud enterprise that make an SQL query work?  :huh:

 

Iam fine with an easy example like 'select 'test' as Test'...

 

$crud->setTable('Orders');
$crud->columns(['Productname','Price','CustomerId']);
$crud->callbackColumn('CustomerId', 'getContent1');
function getContent1($value,$row) {
$sql = "SELECT 'Test' as Test";
$result = $this->db->query($sql)->row();
return $result->Test;
}
 
Best regards

Gregor

Amit Shah

Amit Shah
  • profile picture
  • Member

Posted 21 November 2017 - 17:25 PM

Well my friend, you surely can use it inside the functiot .. but i will rather recommend you making a call to a model. $this - since inside a function, will surely fail. There - you will rather need to get instance of $CI ... and instead of this - make the relevant changes. Also, making a call inside model will be much easier then in here.


beachballer

beachballer
  • profile picture
  • Member

Posted 21 November 2017 - 22:25 PM

Dear all,

 

I need an example please.

Best regards

Gregor


farazu101

farazu101
  • profile picture
  • Member

Posted 22 March 2018 - 03:33 AM

We;; DarlStalker is correct .. 

let me take u a bit deeper in within.

 

when u make a callback column - u get 2 params ($value, $row) .. the $value represents the actual value of the field for which the column is called back for.. and the row is the exact row. With the value or some field from row - you surely can make enough data to query the other table, get back with desired output .. and there you have .. the option to get desired output.

 

Happy GCing :)

 

Will $row contains data from all joined tables? 

 

For Example.

 

 

$crud->setTable('Orders');

$crud->columns(['Productname','Price','CustomerId']);
$crud->setrelation('CustomerId','Customer','{Firstname} {Lastname}');
 
$crud->callbackColumn('Price', function ($value, $row) {

        return "<a href='/example2/customers.php#/edit/" . $row->CustomerId."' target='frame'>".$row->Firstname."</a>";

});
$output = $crud->render();

 

Here

 

setting relation for CustomerId

setting callback for Price

 

Question is will I be able to get Customer table data inside callBack of Price