Gregor Sotošek
Gregor Sotošek

Reputation: 417

Gridfield not populate with ArrayList when not in public function getCMSFields

When I create GridField within admin console - everything is ok - I cam populate gridfield via classic method (ex. Member::get() - - or via ArrayList -

$al1 = new ArrayList();
$records = DB::query("SELECT * from Member where id<10");
while ($rec = $records->next()) {
    $al1->push(new ArrayData($rec));
}       
$grid = new GridField('Pages', 'All pages', $al1)

Both methods are working ok. However, If I try to create GridField on user page - - presented in a form - - somehow the second method (where GridField should be populated by ArrayList - is not working).

$gridField = new GridField('pages1', 'All pages1', Member::get(), $config);

- woks ok, but the method where I create ArrayList old-fashioned way:

$al = new ArrayList();
$records = DB::query("SELECT * from Member where id<10");
while ($rec1 = $records->next()) {
    $al->push(new ArrayData($rec));
}

I get an error when I try to render gridfield through:

return new Form($this, "AllSubmissions", new FieldList($gridField), new FieldList());

The error I am getting is:

[Warning] Missing argument 1 for ArrayData::__construct() GET /ss340/gridfield-test/gridfield-underr-grid/ Line 27 in C:\wamp\www\ss340\framework\view\ArrayData.php

Since I need data from external database to populate gridfield on non admin pages, I am desperate to get the solution for this. If someone can provide me alternative method to show/edit tabular data in Silverstripe - -would appreciate very much.

Upvotes: 0

Views: 275

Answers (2)

Fatal Error
Fatal Error

Reputation: 1024

I just looked up your error. It comes from the gridfield that tries to use this function:

public function getDisplayFields($gridField) {
    if(!$this->displayFields) {
        return singleton($gridField->getModelClass())->summaryFields();
    }
    return $this->displayFields;
}

If you are giving an ArrayList with ArrayData in it, it is trying to create a singleton of ArrayData. This causes an error because ArrayData expects an object or an array.

My oppinion is still to use my old answer, this would give you a DataList and you won't have to go through the trouble.

Old Answer

Why go through all the trouble and not just make use of SilverStripe's ORM with SearchFilters?

$dbConfig = [
    "type" => 'MySQLDatabase',
    "server" => 'localhost',
    "username" => '',
    "password" => '',
    "database" => '',
    "path" => '',
]; // fill this array with your other database configuration

//connect
DB::connect($dbConfig);

$members = Member::get()->filter('ID:LessThan', 10);

//reset to your own database
global $databaseConfig;
DB::connect($databaseConfig);

One last note; when 'developing' it is recommended to put SilverStripe in 'dev mode'. In the comments you say you are getting a Server Error (500) which indicates your SilverStripe is not in dev mode, or your error_reporting is not enabled. Maybe this could help you doing that.

Upvotes: 1

Dan Hensby
Dan Hensby

Reputation: 1143

OK, from the error you posted:

 * var array
 * see ArrayData::_construct() / protected $array; 
/*
 * @param object|array $value An associative array, or an object with simple properties. 
 * Converts object properties to keys of an associative array. 
 */
public function __construct($value) {
    if (is_object($value)) { 
        $this->array = get_object_vars($value); 
    } elseif (ArrayLib::is_associative($value)) { 
        $this->array = $value; 
    } elseif (is_array($value) && count($value) === 0) { 
        $this->array = array();

This error looks incomplete, but I noticed your submitted code is:

$al = new ArrayList();
$records = DB::query("SELECT * from Member where id<10");
while ($rec1 = $records->next()) {
    $al->push(new ArrayData($rec));
}

But $rec is not defined and so likely you are not passing a valid constructor argument to new ArrayData()

try:

$al = new ArrayList();
$records = DB::query("SELECT * from Member where id<10");
while ($rec = $records->next()) {
    $al->push(new ArrayData($rec));
}

Upvotes: 0

Related Questions