本文整理汇总了PHP中CModel::defineIndexes方法的典型用法代码示例。如果您正苦于以下问题:PHP CModel::defineIndexes方法的具体用法?PHP CModel::defineIndexes怎么用?PHP CModel::defineIndexes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CModel
的用法示例。
在下文中一共展示了CModel::defineIndexes方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getRules
//.........这里部分代码省略.........
$uniqueAttributes[] = $name;
}
}
// Required
if ($config['type'] != AttributeType::Bool && !empty($config['required'])) {
$requiredAttributes[] = $name;
}
// Lengths
if ($config['type'] != AttributeType::Number && $config['type'] != AttributeType::Mixed) {
if (isset($config['length']) && is_numeric($config['length'])) {
$strictLengthAttributes[(string) $config['length']][] = $name;
} else {
// Only worry about min- and max-lengths if a strict length isn't set
if (isset($config['minLength']) && is_numeric($config['minLength'])) {
$minLengthAttributes[(string) $config['minLength']][] = $name;
}
if (isset($config['maxLength']) && is_numeric($config['maxLength'])) {
$maxLengthAttributes[(string) $config['maxLength']][] = $name;
}
}
}
// Compare with other attributes
if (isset($config['compare'])) {
$comparisons = ArrayHelper::stringToArray($config['compare']);
foreach ($comparisons as $comparison) {
if (preg_match('/^(==|=|!=|>=|>|<=|<)\\s*\\b(.*)$/', $comparison, $match)) {
$rules[] = array($name, 'compare', 'compareAttribute' => $match[2], 'operator' => $match[1], 'allowEmpty' => true);
}
}
}
// Regex pattern matching
if (!empty($config['matchPattern'])) {
$rules[] = array($name, 'match', 'pattern' => $config['matchPattern']);
}
}
// If this is a BaseRecord instance, catch any unique/required indexes. We don't validate required BELONGS_TO
// relations because they might not get set until after validation.
if ($model instanceof BaseRecord) {
foreach ($model->defineIndexes() as $config) {
$unique = !empty($config['unique']);
$required = !empty($config['required']);
if ($unique || $required) {
$columns = ArrayHelper::stringToArray($config['columns']);
if ($unique) {
if (count($columns) == 1) {
if (empty($attributes[$columns[0]]['required']) && (isset($attributes[$columns[0]]['null']) && $attributes[$columns[0]]['null'] === false)) {
$uniqueRequiredAttributes[] = $columns[0];
} else {
$uniqueAttributes[] = $columns[0];
}
} else {
$initialColumn = array_shift($columns);
$rules[] = array($initialColumn, 'Craft\\CompositeUniqueValidator', 'with' => implode(',', $columns));
}
}
if ($required) {
$requiredAttributes = array_merge($requiredAttributes, $columns);
}
}
}
}
if ($uniqueAttributes) {
$rules[] = array(implode(',', $uniqueAttributes), 'unique');
}
if ($uniqueRequiredAttributes) {
$rules[] = array(implode(',', $uniqueRequiredAttributes), 'unique', 'allowEmpty' => false);
}
if ($requiredAttributes) {
$rules[] = array(implode(',', $requiredAttributes), 'required');
}
if ($emailAttributes) {
$rules[] = array(implode(',', $emailAttributes), 'email');
}
if ($urlAttributes) {
$rules[] = array(implode(',', $urlAttributes), 'Craft\\UrlValidator', 'defaultScheme' => 'http');
}
if ($urlFormatAttributes) {
$rules[] = array(implode(',', $urlFormatAttributes), 'Craft\\UrlFormatValidator');
}
if ($uriAttributes) {
$rules[] = array(implode(',', $uriAttributes), 'Craft\\UriValidator');
}
if ($strictLengthAttributes) {
foreach ($strictLengthAttributes as $strictLength => $attributeNames) {
$rules[] = array(implode(',', $attributeNames), 'length', 'is' => (int) $strictLength);
}
}
if ($minLengthAttributes) {
foreach ($minLengthAttributes as $minLength => $attributeNames) {
$rules[] = array(implode(',', $attributeNames), 'length', 'min' => (int) $minLength);
}
}
if ($maxLengthAttributes) {
foreach ($maxLengthAttributes as $maxLength => $attributeNames) {
$rules[] = array(implode(',', $attributeNames), 'length', 'max' => (int) $maxLength);
}
}
$rules[] = array(implode(',', array_keys($attributes)), 'safe', 'on' => 'search');
return $rules;
}