CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Team
    • Issues (Github)
    • YouTube Channel
    • Get Involved
    • Bakery
    • Featured Resources
    • Newsletter
    • Certification
    • My CakePHP
    • CakeFest
    • Facebook
    • Twitter
    • Help & Support
    • Forum
    • Stack Overflow
    • IRC
    • Slack
    • Paid Support
CakePHP

C CakePHP 3.8 Red Velvet API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 3.8
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Namespaces

  • Cake
    • Auth
      • Storage
    • Cache
      • Engine
    • Collection
      • Iterator
    • Command
    • Console
      • Exception
    • Controller
      • Component
      • Exception
    • Core
      • Configure
        • Engine
      • Exception
      • Retry
    • Database
      • Driver
      • Exception
      • Expression
      • Schema
      • Statement
      • Type
    • Datasource
      • Exception
    • Error
      • Middleware
    • Event
      • Decorator
    • Filesystem
    • Form
    • Http
      • Client
        • Adapter
        • Auth
      • Cookie
      • Exception
      • Middleware
      • Session
    • I18n
      • Formatter
      • Middleware
      • Parser
    • Log
      • Engine
    • Mailer
      • Exception
      • Transport
    • Network
      • Exception
    • ORM
      • Association
      • Behavior
        • Translate
      • Exception
      • Locator
      • Rule
    • Routing
      • Exception
      • Filter
      • Middleware
      • Route
    • Shell
      • Helper
      • Task
    • TestSuite
      • Fixture
      • Stub
    • Utility
      • Exception
    • Validation
    • View
      • Exception
      • Form
      • Helper
      • Widget
  • None

Classes

  • ArrayContext
  • ContextFactory
  • EntityContext
  • FormContext
  • NullContext

Interfaces

  • ContextInterface

Class EntityContext

Provides a form context around a single entity and its relations. It also can be used as context around an array or iterator of entities.

This class lets FormHelper interface with entities or collections of entities.

Important Keys:

  • entity The entity this context is operating on.
  • table Either the ORM\Table instance to fetch schema/validators from, an array of table instances in the case of a form spanning multiple entities, or the name(s) of the table. If this is null the table name(s) will be determined using naming conventions.
  • validator Either the Validation\Validator to use, or the name of the validation method to call on the table object. For example 'default'. Defaults to 'default'. Can be an array of table alias=>validators when dealing with associated forms.
Cake\View\Form\EntityContext implements Cake\View\Form\ContextInterface uses Cake\ORM\Locator\LocatorAwareTrait
Namespace: Cake\View\Form
Location: View/Form/EntityContext.php

Properties summary

  • $_context protected
    array
    Context data for this object.
  • $_isCollection protected
    boolean

    Boolean to track whether or not the entity is a collection.

  • $_request protected
    Cake\Http\ServerRequest
    The request object.
  • $_rootName protected
    string
    The name of the top level entity/table object.
  • $_tables protected
    array
    A dictionary of tables
  • $_validator protected
    Cake\Validation\Validator[]
    Dictionary of validators.

Inherited Properties

  • _tableLocator

Method Summary

  • __construct() public
    Constructor.
  • _extractMultiple() protected

    Helper method used to extract all the primary key values out of an array, The primary key column is guessed out of the provided $path array

  • _getProp() protected
    Read property values or traverse arrays/iterators.
  • _getTable() protected
    Get the table instance from a property path
  • _getValidator() protected

    Get the validator associated to an entity based on naming conventions.

  • _prepare() protected
    Prepare some additional data from the context.
  • _schemaDefault() protected
    Get default value from table schema for given entity field.
  • attributes() public
    Get an associative array of other attributes for a field name.
  • entity() public
    Fetch the entity or data value for a given path
  • error() public
    Get the errors for a given field
  • fieldNames() public
    Get the field names from the top level entity.
  • getMaxLength() public
    Get field length from validation
  • getRequiredMessage() public
  • hasError() public
    Check whether or not a field has an error attached to it
  • isCreate() public
    Check whether or not this form is a create or update.
  • isPrimaryKey() public
    Returns true if the passed field name is part of the primary key for this context
  • isRequired() public
    Check if a field should be marked as required.
  • leafEntity() protected
    Fetch the terminal or leaf entity for the given path.
  • primaryKey() public
    Get the primary key data for the context.
  • type() public
    Get the abstract field type for a given field name.
  • val() public
    Get the value for a given path.

Method Detail

__construct() public ¶

__construct( Cake\Http\ServerRequest $request , array $context )

Constructor.

Parameters
Cake\Http\ServerRequest $request
The request object.
array $context
Context info.

_extractMultiple() protected ¶

_extractMultiple( array|Traversable $values , array $path )

Helper method used to extract all the primary key values out of an array, The primary key column is guessed out of the provided $path array

Parameters
array|Traversable $values
The list from which to extract primary keys from
array $path
Each one of the parts in a path for a field name
Returns
array|null

_getProp() protected ¶

_getProp( mixed $target , string $field )

Read property values or traverse arrays/iterators.

Parameters
mixed $target
The entity/array/collection to fetch $field from.
string $field
The next field to fetch.
Returns
mixed

_getTable() protected ¶

_getTable( array $parts , boolean $fallback = true )

Get the table instance from a property path

Parameters
array $parts
Each one of the parts in a path for a field name
boolean $fallback optional true

Whether or not to fallback to the last found table when a non-existent field/property is being encountered.

Returns
Cake\ORM\Table|boolean
Table instance or false

_getValidator() protected ¶

_getValidator( array $parts )

Get the validator associated to an entity based on naming conventions.

Parameters
array $parts
Each one of the parts in a path for a field name
Returns
Cake\Validation\Validator

_prepare() protected ¶

_prepare( )

Prepare some additional data from the context.

If the table option was provided to the constructor and it was a string, TableLocator will be used to get the correct table instance.

If an object is provided as the table option, it will be used as is.

If no table option is provided, the table name will be derived based on naming conventions. This inference will work with a number of common objects like arrays, Collection objects and ResultSets.

Throws
RuntimeException
When a table object cannot be located/inferred.

_schemaDefault() protected ¶

_schemaDefault( array $parts )

Get default value from table schema for given entity field.

Parameters
array $parts
Each one of the parts in a path for a field name
Returns
mixed

attributes() public ¶

attributes( string $field )

Get an associative array of other attributes for a field name.

Parameters
string $field
A dot separated path to get additional data on.
Returns
array
An array of data describing the additional attributes on a field.
Implementation of
Cake\View\Form\ContextInterface::attributes()

entity() public ¶

entity( array|null $path = null )

Fetch the entity or data value for a given path

This method will traverse the given path and find the entity or array value for a given path.

If you only want the terminal Entity for a path use leafEntity instead.

Parameters
array|null $path optional null

Each one of the parts in a path for a field name or null to get the entity passed in constructor context.

Returns
Cake\Datasource\EntityInterface|Traversable|array|boolean
Throws
RuntimeException
When properties cannot be read.

error() public ¶

error( string $field )

Get the errors for a given field

Parameters
string $field
A dot separated path to check errors on.
Returns
array
An array of errors.
Implementation of
Cake\View\Form\ContextInterface::error()

fieldNames() public ¶

fieldNames( )

Get the field names from the top level entity.

If the context is for an array of entities, the 0th index will be used.

Returns
array
Array of fieldnames in the table/entity.
Implementation of
Cake\View\Form\ContextInterface::fieldNames()

getMaxLength() public ¶

getMaxLength( string $field )

Get field length from validation

Parameters
string $field
The dot separated path to the field you want to check.
Returns
integer|null

getRequiredMessage() public ¶

getRequiredMessage( $field )

hasError() public ¶

hasError( string $field )

Check whether or not a field has an error attached to it

Parameters
string $field
A dot separated path to check errors on.
Returns
boolean
Returns true if the errors for the field are not empty.
Implementation of
Cake\View\Form\ContextInterface::hasError()

isCreate() public ¶

isCreate( )

Check whether or not this form is a create or update.

If the context is for a single entity, the entity's isNew() method will be used. If isNew() returns null, a create operation will be assumed.

If the context is for a collection or array the first object in the collection will be used.

Returns
boolean
Implementation of
Cake\View\Form\ContextInterface::isCreate()

isPrimaryKey() public ¶

isPrimaryKey( string $field )

Returns true if the passed field name is part of the primary key for this context

Parameters
string $field

A dot separated path to the field a value is needed for.

Returns
boolean
Implementation of
Cake\View\Form\ContextInterface::isPrimaryKey()

isRequired() public ¶

isRequired( string $field )

Check if a field should be marked as required.

Parameters
string $field
The dot separated path to the field you want to check.
Returns
boolean
Implementation of
Cake\View\Form\ContextInterface::isRequired()

leafEntity() protected ¶

leafEntity( array|null $path = null )

Fetch the terminal or leaf entity for the given path.

Traverse the path until an entity cannot be found. Lists containing entities will be traversed if the first element contains an entity. Otherwise the containing Entity will be assumed to be the terminal one.

Parameters
array|null $path optional null

Each one of the parts in a path for a field name or null to get the entity passed in constructor context.

Returns
array
Containing the found entity, and remaining un-matched path.
Throws
RuntimeException
When properties cannot be read.

primaryKey() public ¶

primaryKey( )

Get the primary key data for the context.

Gets the primary key columns from the root entity's schema.

Returns
array
Implementation of
Cake\View\Form\ContextInterface::primaryKey()

type() public ¶

type( string $field )

Get the abstract field type for a given field name.

Parameters
string $field
A dot separated path to get a schema type for.
Returns
string|null
An abstract data type or null.
See
Cake\Database\Type
Implementation of
Cake\View\Form\ContextInterface::type()

val() public ¶

val( string $field , array $options = [] )

Get the value for a given path.

Traverses the entity data and finds the value for $path.

Parameters
string $field
The dot separated path to the value.
array $options optional []

Options: - default: Default value to return if no value found in request data or entity. - schemaDefault: Boolean indicating whether default value from table schema should be used if it's not explicitly provided.

Returns
mixed
The value of the field or null on a miss.
Implementation of
Cake\View\Form\ContextInterface::val()

Methods used from Cake\ORM\Locator\LocatorAwareTrait

getTableLocator() public ¶

getTableLocator( )

Gets the table locator.

Returns
Cake\ORM\Locator\LocatorInterface

setTableLocator() public ¶

setTableLocator( Cake\ORM\Locator\LocatorInterface $tableLocator )

Sets the table locator.

Parameters
Cake\ORM\Locator\LocatorInterface $tableLocator
LocatorInterface instance.
Returns

$this

tableLocator() public deprecated ¶

tableLocator( Cake\ORM\Locator\LocatorInterface $tableLocator = null )

Sets the table locator. If no parameters are passed, it will return the currently used locator.

Deprecated
3.5.0 Use getTableLocator()/setTableLocator() instead.
Parameters
Cake\ORM\Locator\LocatorInterface $tableLocator optional null
LocatorInterface instance.
Returns
Cake\ORM\Locator\LocatorInterface

Magic methods inherited from Cake\View\Form\ContextInterface

getMaxLength(), getRequiredMessage()

Properties detail

$_context ¶

protected array

Context data for this object.

$_isCollection ¶

protected boolean

Boolean to track whether or not the entity is a collection.

false

$_request ¶

protected Cake\Http\ServerRequest

The request object.

$_rootName ¶

protected string

The name of the top level entity/table object.

$_tables ¶

protected array

A dictionary of tables

[]

$_validator ¶

protected Cake\Validation\Validator[]

Dictionary of validators.

[]
Follow @CakePHP
#IRC
OpenHub
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Logos & Trademarks
  • Community
  • Team
  • Issues (Github)
  • YouTube Channel
  • Get Involved
  • Bakery
  • Featured Resources
  • Newsletter
  • Certification
  • My CakePHP
  • CakeFest
  • Facebook
  • Twitter
  • Help & Support
  • Forum
  • Stack Overflow
  • IRC
  • Slack
  • Paid Support

Generated using CakePHP API Docs