Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fluid Mode With Arrays #135

Open
marcus-at-localhost opened this issue Jul 2, 2024 · 0 comments
Open

Fluid Mode With Arrays #135

marcus-at-localhost opened this issue Jul 2, 2024 · 0 comments

Comments

@marcus-at-localhost
Copy link
Contributor

marcus-at-localhost commented Jul 2, 2024

Hi,
I'm building a DB in fluid mode from a JSON object.
In my model, the only $fieldConf entry is the ID field, any other column is added at runtime. But not the field that is an array.
It crashes here:

elseif (strlen($val)>10 && strtotime($val)) $type = $schema::DT_DATETIME;

Unfortunately, I can't use a custom setter/getter onset('unknown_fieldname', ...) in the model, since I don't know the name of the field.

Is there an elegant way to set this up to save and retrieve objects in fluid mode?

Edit:

For now I solved it with a custom cortex method that takes in the dataset and creates onset and onget hooks on the fly.

CSV Model

namespace Model;

class Csv extends Base
{
	protected
		$db = 'TemplateDB', // F3 hive key of a valid DB object
		$table = 'csv',     // the DB table to work on
		$fluid = true,
		$fieldConf = [
			'checklistID' => [
				'type' => 'VARCHAR128',
				'index' => true,
				'unique' => true,
				'belongs-to-one' => ['\Model\Checklists', 'checklistID']
			],
		];

	public function setupFluidHooks($fields)
	{
		foreach ((array)$fields as $key => $field) {
			if (is_array($field)) {
				$this->onset($key, function ($self, $val) {
					return json_encode($val);
				});
				$this->onget($key, function ($self, $val) {
					return json_decode($val, true);
				});
			}
		}
	}
}
public function checklistsToDatabase(array $csv, string $checklistID, string $templateID): \Model\Csv
{
	\Model\Csv::setup();

	$csvModel = new \Model\Csv();
	$csvModel->load(['checklistID = ?', $checklistID]);
	$csvModel->setupFluidHooks($csv);
	$csvModel->copyFrom($csv);
	$csvModel->save();
	return $csvModel;
}

This was the least intrusive, but maybe there is a better way?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant