문제

I'm working with PHP Activerecords. I'm trying to make it work with a table that has column names which resemble an array:

tablename {
  id: int,
  column[1]: text,
  column[2]: text,
  ...
  column[10]: text
}

I picked array-like columns names as the data contained in this table can vary.

Now when I want to use the data in the column I would use the following statement

$tabelname->id

this works and will return the id; When I use the following code:

$tabelname->column[1]

I get the following error: PHP Fatal error: Uncaught exception 'ActiveRecord\UndefinedPropertyException' with message 'Undefined property: Tablename->column

For display purposes I can work around this problem using the following code:

$tempdata = array();
foreach(tablename->attributes() as $key ->$value)
{
  $tempdata[$key] = $value
}

But this way I can't update data in the table. Does anyone have a solution how to access the data for both reading and writing?

Mysql: Serverversie: 5.1.41-3ubuntu12.8

PHP: PHP Version 5.3.2-1ubuntu4.14

도움이 되었습니까?

해결책

What I didn't expect to work did:

I changed the column names from column[1] to column_1. Then in my code I accessed them via the following method:

$key = 1; // this is coming from a parameter
$columnname = 'column_' . $key;
$table->$columname = "new value"; // I didn't expect a variable to work as an attribute name
$table->save();
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top