PHP code example of pointybeard / symphony-classmapper
1. Go to this page and download the library: Download pointybeard/symphony-classmapper library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
pointybeard / symphony-classmapper example snippets
declare(strict_types=1);
\Classmapper;
Classmapper\create(
'Article', // Name of class to be created
'articles' // Handle of section to map
);
// Create a new article
$article = new Article;
$article->title('My Article');
$article->save();
// Classmapper also supports method chaining like so
(new Article)
->title('My New Article')
->save()
;
// Get article with id of 1
$article = Article::loadFromId(1);
// Iterate over all articles
foreach(Article::all() as $article) {
printf("%d: %s\r\n", $article->id, $article->title);
}
# Check if it was modified
$article->hasBeenModified();
# Get the XML representation of your Article
$article->toXml();
# Remove the article
$article->delete()
# Alternatively, you can delete entries like this
Article::loadFromId(3)->delete();
namespace Your\Project\Namespace;
use pointybeard\Symphony\Classmapper;
final class Article extends Classmapper\AbstractModel
{
use Classmapper\Traits\HasModelTrait;
}
...
public function getSectionHandle(): string
{
return 'articles';
}
...
...
# Create a mapping for the Author field, mapping the id to 'authorId'
protected static function getCustomFieldMapping() {
return [
'author' => [
'databaseFieldName' => 'relation_id',
'classMemberName' => 'authorId',
'flags' => self::FLAG_INT
],
];
}
# Create a method that allows easy retrieval of an Author object.
# Note, this assumes an Author class model exists.
public function author() {
return Author::fetchFromId($this->authorId);
}
...
use pointybeard\Symphony\Classmapper;
class Articles extends Classmapper\AbstractModel implements Classmapper\Interfaces\SortableModelInterface {
use Classmapper\Traits\HasSortableModelTrait;
...
}
...
protected static function fetchSQL($where = 1)
{
return sprintf('
SELECT SQL_CALC_FOUND_ROWS
t.entry_id as `id`,
t.value as `title`,
f.file as `file`,
a.value as `available`
FROM `tbl_entries_data_%d` AS `t`
INNER JOIN `tbl_entries_data_%d` AS `f` ON f.entry_id = t.entry_id
LEFT JOIN `tbl_entries_data_%d` AS `a` ON a.entry_id = f.entry_id
WHERE %s
ORDER BY t.entry_id ASC',
self::$sectionFields['title'],
self::$sectionFields['file'],
self::$sectionFields['available'],
$where
);
}
...
...
protected function getData()
{
$data = parent::getData();
// Check if anything has changed and, if so, set the new modified date
if($this->hasBeenModified()) {
$data['modified'] = 'now';
}
return $data;
}
...
use pointybeard\Symphony\Classmapper;
class Articles extends Classmapper\AbstractModel implements Classmapper\Interfaces\FilterableModelInterface {
use Classmapper\Traits\HasFilterableModelTrait;
...
}
Article::fetch(...)->each(function ($article) {
// do something with $article here
});
foreach(Article::fetch(...) as $article) {
// do something with $article here
}
public function __construct(
string $field,
$value,
int $type = \PDO::PARAM_STR,
string $comparisonOperator = self::COMPARISON_OPERATOR_EQ,
string $operator = self::OPERATOR_AND
)
public function __construct(
$field,
array $values,
string $operator = self::OPERATOR_AND
)
public function __construct(
$field,
string $operator = self::OPERATOR_AND
)
public function __construct(
string $field,
string $comparisonOperator = self::COMPARISON_OPERATOR_EQ,
string $operator = self::OPERATOR_AND
)
## Create an instance of the Article model
$article = new Article;
## Append filters with appendFilter(). Note method chaining is supported
$article
->appendFilter(Classmapper\FilterFactory::build('Basic', 'published', 'Yes'))
->appendFilter(Classmapper\FilterFactory::build('Now', 'dateCreatedAt', Classmapper\Filters\Basic::COMPARISON_OPERATOR_LT))
;
## Returns the results
$result = $article->filter();
## Optionally clear the filters from this instance
$article->clearFilters();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.