Cherrycake
ExamplesGithub
version 2.x alpha
version 2.x alpha
  • Introduction
  • Status
  • Changelog
  • Migration
  • Architecture
    • Basics
    • Modules
    • Classes
    • Lifecycle
      • Deep lifecycle
    • Performance
    • Security
    • Patterns
      • Files structure
    • Items
    • Server requirements
  • Guide
    • Getting started
      • Skeleton start
      • Docker start
    • Modules guide
    • Classes guide
    • Actions guide
      • Complex actions
      • Variable path components
      • Accept GET or POST parameters
      • Getting the URL of an action
      • Cached actions
      • Brute force attacks
    • Patterns guide
      • Passing variables to a pattern
      • Nested patterns
      • Cached patterns
    • Cache guide
      • Time To Live
      • Using cache
      • Lists
      • Queues
      • Pools
    • Database guide
      • Basic queries
      • Prepared queries
      • Cached queries
      • Cache key naming
      • Removing queries from cache
    • Items guide
      • Item cache
      • Item lists
      • Items custom filters
      • Items custom ordering
      • Mixing filters and ordering
      • Items with relationships
      • Items cache
    • HtmlDocument guide
    • Css and Javascript guide
      • Modules injecting CSS and JavaScript
    • Session guide
    • Login guide
      • Creating a complete login workflow
    • Locale guide
      • Multilingual texts
      • Domain based site localization
    • Log guide
      • Loading Log events from the database
    • Stats guide
      • Stats events with additional dimensions
      • Loading Stats events from the database
    • Janitor guide
      • Janitor tasks configuration files
    • Command line interface
    • Debugging
  • Reference
    • Core modules
      • Actions
        • Actions methods
      • Browser
      • Cache
        • Cache methods
      • Css
        • Css methods
      • Database
      • Email
      • Errors
      • HtmlDocument
        • HtmlDocument methods
      • ItemAdmin
      • Janitor
        • Janitor methods
      • Javascript
        • Javascript methods
      • Locale
        • Locale methods
      • Log
        • Log methods
      • Login
        • Login methods
      • Output
        • Output methods
      • Patterns
        • Patterns methods
      • Security
        • Security methods
      • Session
        • Session methods
      • Stats
        • Stats methods
      • SystemLog
      • TableAdmin
      • Translation
      • Validate
    • Core classes
      • Action
        • Action methods
        • Action properties
      • AjaxResponseJson
      • BasicObject
        • BasicObject methods
      • CacheProvider
        • CacheProvider methods
      • Color
      • DatabaseProvider
        • DatabaseProvider methods
      • DatabaseResult
        • DatabaseResult methods
        • DatabaseResult properties
      • DatabaseRow
      • Engine
        • Engine methods
        • Engine properties
      • Gradient
      • Item
        • Item methods
        • Item properties
      • Items
        • Items methods
        • Items properties
      • Image
      • JanitorTask
        • JanitorTask methods
        • JanitorTask properties
      • LogEvent
        • LogEvent methods
        • LogEvent Properties
      • LogEvents
        • LogEvents methods
      • Module
        • Module methods
        • Module properties
      • Response
      • Request
        • Request methods
      • RequestParameter
        • RequestParameter methods
      • RequestPathComponent
        • RequestPathComponent methods
      • Result
      • StatsEvent
        • StatsEvent properties
      • StatsEvents
        • StatsEvents methods
      • SystemLogEvent
        • SystemLogEvent methods
        • SystemLogEvent properties
      • SystemLogEvents
        • SystemLogEvents methods
  • Code conventions
  • License
  • Extras
Powered by GitBook
On this page

Was this helpful?

  1. Guide

Items guide

Using Items in your Cherrycake application brings you many benefits when interacting with the primordial objects of your app, like optimized loading, storage, caching and embedded security mechanisms.

PreviousRemoving queries from cacheNextItem cache

Last updated 5 years ago

Was this helpful?

Items are Cherrycake's conceptualization of the fundamental objects stored in a database. For example, in an e-commerce site, a product would be an Item, but also would a user, a product category or an invoice.

Creating an Item class

Items always come from a database table, so let's imagine we have a database of movies and we want to define an to work with the movies that are stored in our database, in a table called movies with the following fields:

Field name

Specs

id

unsigned int auto_increment primary key

The unique id to identify movies.

title

varchar

The name of the movie.

summary

text

A summary of the movie plot.

year

year

The year the movie was released.

dateAdded

datetime

The date and time the movie was added to the database.

directorId

unsigned int

The id of the director in the directors table.

You can get an SQL script to create this table in the , in the file.

Items are that extend the Cherrycake's core class, so we create the Movie class in the file /classes/Movie.class.php, and it looks like this:

<?php

namespace CherrycakeApp;

class Movie extends \Cherrycake\Item {
    protected $tableName = "movies";
    protected $fields = [
        "id" => [
            "type" => \Cherrycake\DATABASE_FIELD_TYPE_INTEGER
        ],
        "title" => [
            "type" => \Cherrycake\DATABASE_FIELD_TYPE_STRING
        ],
        "summary" => [
            "type" => \Cherrycake\DATABASE_FIELD_TYPE_TEXT
        ],
        "year" => [
            "type" => \Cherrycake\DATABASE_FIELD_TYPE_YEAR
        ],
        "imdbRating" => [
            "type" => \Cherrycake\DATABASE_FIELD_TYPE_FLOAT
        ]
    ];
}

We set some properties of the class to configure it:

  • tableName The name of the table where the items are stored.

You can also set this other properties if you'll be using values different from the defaults:

  • databaseProviderName The database provider name where this items are stored. The default is main

  • idFieldName The name of the field that contains values to uniquely identify each item in the table. Defaults to id

With this we've already created a functional Item that can now represent a movie in our app with the added benefits of using Cherrycake Items.

Now, what can you do with your new Movie class? Let's see how to create a Movie object we can manipulate. Let's say we want to load the movie with id 15:

$movie = new Movie([
    "loadMethod" => "fromId",
    "id" => 15
]);
echo "{$movie->title} ({$movie->year})";
Brainstorm (1983)
$movie->update([
    "imdbRating" => 8.7
]);
$movie->imdbRating = 8.7;
$movie->update();
$movie->delete();

fields A hash array to specify the field names and field types of the table. See for all the available field types. See for more keys you can use here to customize how your Item works.

Field values for an are accessed just like regular properties, like this:

See this example working in the site.

We can also update items on the database by using , for example:

Changing an Item's property manually and then calling without any parameters also works. This will do the same as the example above:

To remove an item from the database, use the method:

Item
Cherrycake documentation examples
Item
Cherrycake documentation examples repository
/install/database/movies.sql
Item
App classes
Item::update
Item::update
Item::delete
Item::$fields
Database constants