我有一个名为SYSTEM_PARAMS
的表,如下所示
+------------+------------------+------+-----+---------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------------------+-----------------------------+
| name | varchar(50) | NO | | NULL | |
| value | varchar(100) | YES | | NULL | |
| updated_at | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| created_at | timestamp | NO | | 0000-00-00 00:00:00 | |
| created_by | int(11) | NO | | NULL | |
| updated_by | int(11) unsigned | YES | | 0 | |
+------------+------------------+------+-----+---------------------+-----------------------------+
其中我有必须运行的cron作业的所有名称,并使用当前正在运行的JobId
更新特定作业名称的值,表格中没有定义primaryKey
看到上面的模式,所以我在模型中定义了方法primaryKey()
,如下所示
public function primaryKey($asArray=FALSE) {
return 'name';
}
但这给了我一个错误,说我可以将静态方法定义为非静态方法,我在这里做错了什么。
PHP致命错误:无法制作静态方法 yii \ db \ ActiveRecord :: primaryKey()类中的非静态 常见\模型\ SystemParams
答案 0 :(得分:5)
正是它所说的。
public static function primaryKey()
{
return ['name'];
}
是ActiveRecord类中的静态方法。
如果你想覆盖它,你也必须使它静止。
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
public class JsonTable {
JsonParser parser;
public JsonTable( String filename ) {
try {
JsonFactory f = new JsonFactory();
InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream( filename );
parser = f.createParser(stream);
} catch ( IOException e ) {
e.printStackTrace();
}
}
public String getTable() throws Exception {
try {
StringBuilder builder = new StringBuilder();
List<String> header = new ArrayList<>();
if ( parser.nextToken() != JsonToken.START_ARRAY ) {
throw new RuntimeException( "Expected an array of objects." );
}
while ( parser.nextToken().equals(JsonToken.START_OBJECT) ) {
buildRow(header,builder);
}
if ( parser.currentToken() != JsonToken.END_ARRAY ) {
throw new RuntimeException( "Expected end of JSON array, got: " + parser.currentToken() );
}
wrapRowsWithTable(header,builder);
return builder.toString();
} finally {
parser.close();
}
}
private void wrapRowsWithTable( List<String> columns, StringBuilder builder ) {
StringBuilder header = new StringBuilder();
header.append("<table><thead><tr>");
for( String column : columns ) {
header.append("<th>" + column + "</th>");
}
header.append("</tr></thead>");
builder.insert(0,header.toString());
builder.append("</table>");
}
private void buildRow( List<String> header, StringBuilder builder ) throws IOException {
int index = 0;
builder.append("<tr>");
while(parser.nextToken() != JsonToken.END_OBJECT)
switch( parser.currentToken() ) {
case FIELD_NAME:
if( header.size() > index ) {
if( !header.get(index).equals( parser.getCurrentName() ) ) {
throw new RuntimeException( "Expected field: " + header.get(index) + ", found: " + parser.getCurrentName() );
}
} else {
header.add(parser.getCurrentName());
}
index++;
break;
case VALUE_NULL:
case VALUE_TRUE:
case VALUE_FALSE:
case VALUE_NUMBER_INT:
case VALUE_NUMBER_FLOAT:
case VALUE_STRING:
builder.append("<td>" + parser.getText() + "</td>");
break;
case VALUE_EMBEDDED_OBJECT:
throw new RuntimeException( "Expecting an array of objects with no nested objects." );
default:
throw new RuntimeException( "Unexpected token: " + parser.currentToken() );
}
}
}
PS。它必须返回数组。请参阅this note。