tattersoftware / codeigniter4-relations

Entity relationships for CodeIgniter 4
MIT License
87 stars 20 forks source link

Setting $with as string with ModelTrait #7

Closed sfadschm closed 3 years ago

sfadschm commented 3 years ago

The readme states, that the ModelTrait can be used as follows:

protected $with = 'groups';
// or
protected $with = ['groups', 'permissions'];

When using the first option and using a string for a single relation, ModelTrait->getWith throws an error because it is supposed to return an array:

https://github.com/tattersoftware/codeigniter4-relations/blob/b964ae2410da9b93c14167b2d396d1768cec9572/src/Traits/ModelTrait.php#L83-L87

Should $this->with be casted to array maybe?

sfadschm commented 3 years ago

Or maybe just change the readme :)

MGatner commented 3 years ago

You are correct. Maybe something like the handling above that?

// Force a single table name into an array
if (! is_array($with))
{
    $with = [$with];
}
sfadschm commented 3 years ago

Yeah this looks much better than just casting or extending the one-liner.

sfadschm commented 3 years ago

On second thought: If $with is null, then is_array returns false too and will generate [null] instead of [] with are unequal according to var_dumpat least. Does this matter?

MGatner commented 3 years ago

Yes sorry, that was pasted from ModelTrait. I'm on mobile, writing code is hard.

Basically getWith() should always return string[], so either [] or an array of strings.

I won't be on desktop for another 12 hours or so but feel free to send a PR if you want to fix this.